Last active
August 29, 2015 14:11
-
-
Save ksol/362a899e98028768119f 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({ | |
| model: function() { | |
| return this.get('store').find('post')); | |
| }, | |
| 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.ArrayController.extend({ | |
| // Are we currently fetching more data? | |
| loadingMore: false, | |
| // The current page (API-wise) we're on | |
| page: 1, | |
| // The total number of pages, filled first by the route | |
| // and later updated via the `loadMore` action | |
| totalPages: null, | |
| // Is there more data to load ? | |
| hasMore: (function() { | |
| return this.get('page') < this.get('totalPages'); | |
| }).property('page', 'totalPages'), | |
| actions: { | |
| loadMore: function() { | |
| var _this = this; | |
| // We're fetching the next page of results | |
| var query = { | |
| page: this.get('page') + 1, | |
| }; | |
| // We're starting to fetch data now | |
| this.set('loadingMore', true); | |
| this.get('store').find('post', query).then(function(data) { | |
| // We're done fetching data for the moment | |
| _this.set('loadingMore', false); | |
| // `data` is an Ember Data powered array-like. | |
| // We're merging its content with the models already fetched... | |
| _this.get('model').pushObjects(data.get('model')); | |
| // Note: in one project, I had to manually tell the controller that `model` had changed. | |
| // You should probably not have to this by yourself: | |
| // _this.notifyPropertyChange('model'); | |
| // And we're updating the current page we're on | |
| // as well as the total page count (in case it changed). | |
| _this.incrementProperty('page'); | |
| _this.set('totalPages', data.get('meta.totalPages')); | |
| }, function() { | |
| // We're done fetching data for the moment | |
| _this.set('loadingMore', false); | |
| // Additional error handling... | |
| }); | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment