Skip to content

Instantly share code, notes, and snippets.

@jagthedrummer
Created July 21, 2013 20:26
Show Gist options
  • Save jagthedrummer/6049840 to your computer and use it in GitHub Desktop.
Save jagthedrummer/6049840 to your computer and use it in GitHub Desktop.
var get = Ember.get;
/**
@extends Ember.Mixin
Implements common pagination management properties for controllers.
*/
Ember.PaginationSupport = Ember.Mixin.create({
/**
*/
total: 0,
/**
*/
rangeStart: 0,
/**
*/
rangeWindowSize: 10,
/**
*/
rangeStop: Ember.computed('total', 'rangeStart', 'rangeWindowSize', function() {
var rangeStop = get(this, 'rangeStart') + get(this, 'rangeWindowSize'),
total = get(this, 'total');
if (rangeStop < total) {
return rangeStop;
}
return total;
}).cacheable(),
/**
*/
page: Ember.computed('rangeStart', 'rangeWindowSize', function() {
return (get(this, 'rangeStart') / get(this, 'rangeWindowSize')) + 1;
}).cacheable(),
/**
*/
totalPages: Ember.computed('total', 'rangeWindowSize', function() {
return Math.ceil(get(this, 'total') / get(this, 'rangeWindowSize'));
}).cacheable(),
/**
*/
hasPrevious: Ember.computed('rangeStart', function() {
return get(this, 'rangeStart') > 0;
}).cacheable(),
/**
*/
hasNext: Ember.computed('rangeStop', 'total', function() {
return get(this, 'rangeStop') < get(this, 'total');
}).cacheable(),
/**
*/
didRequestRange: Ember.K,
/**
*/
nextPage: function() {
if (get(this, 'hasNext')) {
this.incrementProperty('rangeStart', get(this, 'rangeWindowSize'));
}
},
/**
*/
previousPage: function() {
if (get(this, 'hasPrevious')) {
this.decrementProperty('rangeStart', get(this, 'rangeWindowSize'));
}
},
rangeDidChange: Ember.observer(function() {
this.didRequestRange(get(this, 'rangeStart'), get(this, 'rangeStop'));
}, 'rangeStart', 'rangeStop')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment