Skip to content

Instantly share code, notes, and snippets.

@michaelorionmcmanus
Created April 29, 2014 00:33
Show Gist options
  • Save michaelorionmcmanus/11387942 to your computer and use it in GitHub Desktop.
Save michaelorionmcmanus/11387942 to your computer and use it in GitHub Desktop.
simple pagination with query params new
export default Ember.Mixin.create({
// Register all the query parameters that the controller needs to know about.
queryParams: ['_start', '_limit'],
// Store our values.
_start: 0,
_limit: 10,
nextStart: 0,
previousStart: null,
hasPrevious: false,
hasNext: true,
nextStartObserver: function() {
var start = (+this.get('_start')) + (+this.get('_limit'));
this.set('nextStart', start);
}.observes('_limit', '_start', 'resourceTotal'),
hasNextObserver: function() {
var refreshing = this.get('refreshing');
if(this.get('resourceTotal') - this.get('_start') > this.get('_limit')) {
this.set('hasNext', true);
} else {
this.set('hasNext', false);
}
}.observes('_limit', '_start', 'resourceTotal'),
hasPreviousObserver: function() {
if(this.get('_start') >= this.get('_limit')) {
this.set('hasPrevious', true);
} else {
this.set('hasPrevious', false);
}
}.observes('_limit', '_start', 'resourceTotal'),
previousStartObserver: function() {
var start = (+this.get('_start')) - (+this.get('_limit'));
this.set('previousStart', start);
}.observes('_limit', '_start', 'resourceTotal'),
disableNext: function() {
return !this.get('hasNext');
}.property('hasNext'),
disablePrevious: function() {
return !this.get('hasPrevious');
}.property('hasPrevious')
});
export default Ember.Route.extend({
model: function(params) {
return this.store.find('collection', {
'_relations': 'page[id]',
'_start': this.get('param1'),
'_limit': params._limit || 10,
'_sorts': 'id;desc'
});
},
afterModel: function() {
// TODO: Should be querying for only the pages related to this collection.
return this.store.find('page');
},
setupController: function(controller, model) {
var resourceTotal = this.store.metadataFor('collection').total;
controller.set('resourceTotal', resourceTotal);
this._super(controller, model);
},
actions: {
queryParamsDidChange: function() {
this.refresh();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment