Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default Ember.Mixin.create({
- // the keys that represent a property stored on the controller
- // which will be used to lookup any the properties and passed
- // into the model hook via the tableParams object on the first argument
- // of model
- tableParams: ['page', 'limit'],
- // usually what you have the table bound to
- contentKey: 'content',
- // these are used to reset the controller when transitioning in and out
- // of the route during activate and setupController
- tableParamsDefaults: {
- page: 1,
- limit: 25
- },
- actions: {
- pagination: function(params) {
- // if params.page is -1 then an error happened during the previous page
- // so prevent any more pages from loading unless the state is reset
- if(this.controller.get('page') === -1) {
- return false;
- }
- this.controller.incrementProperty('page');
- return this.fetch(params);
- }
- },
- fetch: function(/* params */) {
- return this.model({}).then(function(result) {
- return this.payloadReceived.apply(this, arguments);
- }.bind(this));
- },
- init: function() {
- this._super.apply(this, arguments);
- var fn = this.model;
- // intercepts model calls and adds pagination parameters
- this.model = function(params, transition) {
- params = Ember.mixin(params || {}, {
- tableParams: this.buildTableParams()
- });
- return fn.apply(this, arguments);
- }
- },
- buildTableParams: function() {
- var ctrler = this.controller,
- result = {};
- this.tableParams.forEach(function(key) {
- if(ctrler && ctrler.get(key)) {
- result[key] = ctrler.get(key);
- } else {
- result[key] = Ember.get(this, 'tableParamsDefaults.' + key);
- }
- }, this);
- return result;
- },
- payloadReceived: function(results) {
- results = results || [];
- // we must have hit the result limit, stop firing requests
- if(Ember.get(results, 'length') < this.controller.get('limit')) {
- this.controller.set('page', -1);
- }
- this.controller.get(this.contentKey).pushObjects(results);
- },
- resetTableState: function(clear, exclusion) {
- var ctrler = this.controller,
- exclusion = exclusion || [];
- Ember.assert("A controller must be exist when calling resetTableState", ctrler);
- if(clear) {
- this.controller.set(this.contentKey, []);
- }
- this.tableParams.filter(function(key) {
- return !exclusion.contains(key);
- }).forEach(function(key) {
- ctrler.set(key, this.tableParamsDefaults[key]);
- }, this);
- },
- setupController: function(controller, model) {
- this._super.apply(this, arguments);
- controller.set('tableParams', this.tableParams);
- this.resetTableState();
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement