Skip to content

Instantly share code, notes, and snippets.

@barelyknown
Created June 3, 2015 17:31
Show Gist options
  • Save barelyknown/406a082b63ef9237a882 to your computer and use it in GitHub Desktop.
Save barelyknown/406a082b63ef9237a882 to your computer and use it in GitHub Desktop.
Basic paginating route.
// using ember-jsonapi-resources as its store
import Ember from 'ember';
export default Ember.Route.extend({
defaultOffset: 0,
offset: null,
defaultLimit: 5,
limit: null,
beforeModel(transition) {
this.setProperties({
'offset': transition.queryParams.offset || this.get('defaultOffset'),
'limit': transition.queryParams.limit || this.get('defaultLimit')
});
},
model: function(params) {
return this.store.find('games', {
query: {
'page[offset]': this.get('offset'),
'page[limit]': this.get('limit')
}
});
},
setupController(controller, resources) {
controller.setProperties(this.getProperties('offset', 'limit'));
this._super(controller, resources);
},
queryParams: {
offset: {
refreshModel: true,
as: 'page[offset]'
},
limit: {
refreshModel: true,
as: 'page[limit]'
}
},
actions: {
willTransition: function(transition) {
// reset query params in route and controller if changing page
if (this.routeName != transition.targetName) {
const nullParams = {offset: null, limit: null};
this.setProperties(nullParams);
this.controller.setProperties(nullParams);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment