Advertisement
Guest User

Untitled

a guest
May 29th, 2014
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default Ember.Mixin.create({
  2.     // the keys that represent a property stored on the controller
  3.     // which will be used to lookup any the properties and passed
  4.     // into the model hook via the tableParams object on the first argument
  5.     // of model
  6.     tableParams: ['page', 'limit'],
  7.  
  8.     // usually what you have the table bound to
  9.     contentKey: 'content',
  10.  
  11.     // these are used to reset the controller when transitioning in and out
  12.     // of the route during activate and setupController
  13.     tableParamsDefaults: {
  14.         page:          1,
  15.         limit:         25
  16.     },
  17.  
  18.     actions: {
  19.         pagination: function(params) {
  20.             // if params.page is -1 then an error happened during the previous page
  21.             // so prevent any more pages from loading unless the state is reset
  22.             if(this.controller.get('page') === -1)  {
  23.                 return false;
  24.             }
  25.  
  26.             this.controller.incrementProperty('page');
  27.  
  28.             return this.fetch(params);
  29.         }
  30.     },
  31.  
  32.     fetch: function(/* params */) {
  33.         return this.model({}).then(function(result) {
  34.             return this.payloadReceived.apply(this, arguments);
  35.         }.bind(this));
  36.     },
  37.  
  38.     init: function() {
  39.         this._super.apply(this, arguments);
  40.  
  41.         var fn = this.model;
  42.  
  43.         // intercepts model calls and adds pagination parameters
  44.         this.model = function(params, transition) {
  45.             params = Ember.mixin(params || {}, {
  46.                 tableParams: this.buildTableParams()
  47.             });
  48.  
  49.             return fn.apply(this, arguments);
  50.         }
  51.     },
  52.  
  53.     buildTableParams: function() {
  54.         var ctrler = this.controller,
  55.             result = {};
  56.  
  57.         this.tableParams.forEach(function(key) {
  58.             if(ctrler && ctrler.get(key)) {
  59.                 result[key] = ctrler.get(key);
  60.             } else {
  61.                 result[key] = Ember.get(this, 'tableParamsDefaults.' + key);
  62.             }
  63.         }, this);
  64.  
  65.         return result;
  66.     },
  67.  
  68.     payloadReceived: function(results) {
  69.         results = results || [];
  70.  
  71.         // we must have hit the result limit, stop firing requests
  72.         if(Ember.get(results, 'length') < this.controller.get('limit')) {
  73.             this.controller.set('page', -1);
  74.         }
  75.  
  76.         this.controller.get(this.contentKey).pushObjects(results);
  77.     },
  78.  
  79.     resetTableState: function(clear, exclusion) {
  80.         var ctrler = this.controller,
  81.             exclusion = exclusion || [];
  82.  
  83.         Ember.assert("A controller must be exist when calling resetTableState", ctrler);
  84.  
  85.         if(clear) {
  86.         this.controller.set(this.contentKey, []);
  87.         }
  88.  
  89.         this.tableParams.filter(function(key) {
  90.             return !exclusion.contains(key);
  91.         }).forEach(function(key) {
  92.             ctrler.set(key, this.tableParamsDefaults[key]);
  93.         }, this);
  94.     },
  95.  
  96.     setupController: function(controller, model) {
  97.         this._super.apply(this, arguments);
  98.  
  99.         controller.set('tableParams', this.tableParams);
  100.  
  101.         this.resetTableState();
  102.     }
  103. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement