Skip to content

Instantly share code, notes, and snippets.

@zoghal
Created June 4, 2012 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoghal/2869443 to your computer and use it in GitHub Desktop.
Save zoghal/2869443 to your computer and use it in GitHub Desktop.
Ember.PaginationSupport
App.collectionController = Em.ArrayProxy.create(Ember.PaginationSupport, {
content: [],
fullContent: App.store.findAll(App.Job),
totalBinding: 'fullContent.length',
didRequestRange: function(rangeStart, rangeStop) {
var content = this.get('fullContent').slice(rangeStart, rangeStop);
this.replace(0, this.get('length'), content);
}
});
var get = Ember.get;
/**
@extends Ember.Mixin
Implements common pagination management properties for controllers.
*/
Ember.PaginationSupport = Ember.Mixin.create({
hasPaginationSupport: true,
total: 0,
rangeStart: 0,
rangeWindowSize: 10,
didRequestRange: Ember.K,
rangeStop: function() {
var rangeStop = get(this, 'rangeStart') + get(this, 'rangeWindowSize'),
total = get(this, 'total');
if (rangeStop < total) {
return rangeStop;
}
return total;
}.property('total', 'rangeStart', 'rangeWindowSize').cacheable(),
hasPrevious: function() {
return get(this, 'rangeStart') > 0;
}.property('rangeStart').cacheable(),
hasNext: function() {
return get(this, 'rangeStop') < get(this, 'total');
}.property('rangeStop', 'total').cacheable(),
nextPage: function() {
if (get(this, 'hasNext')) {
this.incrementProperty('rangeStart', get(this, 'rangeWindowSize'));
}
},
previousPage: function() {
if (get(this, 'hasPrevious')) {
this.decrementProperty('rangeStart', get(this, 'rangeWindowSize'));
}
},
page: function() {
return (get(this, 'rangeStart') / get(this, 'rangeWindowSize')) + 1;
}.property('rangeStart', 'rangeWindowSize').cacheable(),
totalPages: function() {
return Math.ceil(get(this, 'total') / get(this, 'rangeWindowSize'));
}.property('total', 'rangeWindowSize').cacheable(),
pageDidChange: function() {
this.didRequestRange(get(this, 'rangeStart'), get(this, 'rangeStop'));
}.observes('total', 'rangeStart', 'rangeStop')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment