Created
December 14, 2014 21:11
-
-
Save ksol/7a1d5c2cf3743bd7e5f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ember from 'ember'; | |
| export default Ember.Route.extend({ | |
| // Here, we're telling ember to "watch" the query parameter "page". | |
| // In this case, if the parameter changes, the model will be fetched again. | |
| // We could map the parameter to a different property name if we wanted. | |
| queryParams: { | |
| page: { | |
| refreshModel: true | |
| } | |
| }, | |
| model: function(params) { | |
| var query = {}; | |
| // If the query parameter "page" is present, we're relaying it to ember data. | |
| // Note: we could also include other parameters, eg. "perPage", if the API handles it. | |
| if(Ember.isPresent(params.page)) { | |
| query.page = params.page; | |
| } | |
| return this.get('store').find('post', query); | |
| }, | |
| // Here, we're passing metadata to the controller - namely, the total number of pages. | |
| // This method will be executed each time the model is reloaded. | |
| setupController: function(controller, model) { | |
| this._super.apply(this, arguments); // Do not forget this call | |
| controller.set('totalPages', model.get('meta.totalPages')); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ember from 'ember'; | |
| export default Ember.Controller.extend({ | |
| // Here, we're telling the controller that the property `page` | |
| // should be "bound" to the query parameter the same name. | |
| // We could map the parameter to a different property name if we wanted. | |
| queryParams: [ | |
| 'page', | |
| ], | |
| // The default value for our page property | |
| page: 1, | |
| // This property will be set by the parent route | |
| totalPages: null, | |
| // The following properties will be used for the display of the pagination links | |
| prevPage: function() { | |
| return this.get('page') - 1; | |
| }.property('page'), | |
| nextPage: function() { | |
| return this.get('page') + 1; | |
| }.property('page'), | |
| isFirstPage: function() { | |
| return this.get('page') === 1; | |
| }.property('page'), | |
| isLastPage: function() { | |
| return this.get('page') >= this.get('totalPages'); | |
| }.property('page', 'totalPages'), | |
| pageRange: function () { | |
| var result = Ember.A(); | |
| for(var i = 1; i <= this.get('totalPages'); i++) { | |
| result.push(i); | |
| } | |
| return result; | |
| }.property('totalPages') | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment