Skip to content

Instantly share code, notes, and snippets.

@michaelorionmcmanus
Created April 29, 2014 00:33
Show Gist options
  • Select an option

  • Save michaelorionmcmanus/11387942 to your computer and use it in GitHub Desktop.

Select an option

Save michaelorionmcmanus/11387942 to your computer and use it in GitHub Desktop.

Revisions

  1. michaelorionmcmanus created this gist Apr 29, 2014.
    49 changes: 49 additions & 0 deletions pagination-mixin.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    export default Ember.Mixin.create({
    // Register all the query parameters that the controller needs to know about.
    queryParams: ['_start', '_limit'],
    // Store our values.
    _start: 0,
    _limit: 10,

    nextStart: 0,
    previousStart: null,

    hasPrevious: false,
    hasNext: true,

    nextStartObserver: function() {
    var start = (+this.get('_start')) + (+this.get('_limit'));
    this.set('nextStart', start);
    }.observes('_limit', '_start', 'resourceTotal'),

    hasNextObserver: function() {
    var refreshing = this.get('refreshing');
    if(this.get('resourceTotal') - this.get('_start') > this.get('_limit')) {
    this.set('hasNext', true);
    } else {
    this.set('hasNext', false);
    }
    }.observes('_limit', '_start', 'resourceTotal'),

    hasPreviousObserver: function() {
    if(this.get('_start') >= this.get('_limit')) {
    this.set('hasPrevious', true);
    } else {
    this.set('hasPrevious', false);
    }
    }.observes('_limit', '_start', 'resourceTotal'),

    previousStartObserver: function() {
    var start = (+this.get('_start')) - (+this.get('_limit'));
    this.set('previousStart', start);
    }.observes('_limit', '_start', 'resourceTotal'),

    disableNext: function() {
    return !this.get('hasNext');
    }.property('hasNext'),

    disablePrevious: function() {
    return !this.get('hasPrevious');
    }.property('hasPrevious')

    });
    28 changes: 28 additions & 0 deletions route.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    export default Ember.Route.extend({

    model: function(params) {
    return this.store.find('collection', {
    '_relations': 'page[id]',
    '_start': this.get('param1'),
    '_limit': params._limit || 10,
    '_sorts': 'id;desc'
    });
    },

    afterModel: function() {
    // TODO: Should be querying for only the pages related to this collection.
    return this.store.find('page');
    },

    setupController: function(controller, model) {
    var resourceTotal = this.store.metadataFor('collection').total;
    controller.set('resourceTotal', resourceTotal);
    this._super(controller, model);
    },

    actions: {
    queryParamsDidChange: function() {
    this.refresh();
    }
    }
    });