Skip to content

Instantly share code, notes, and snippets.

@tcjr
Last active December 25, 2015 06:59

Revisions

  1. tcjr revised this gist Oct 11, 2013. 3 changed files with 23 additions and 3 deletions.
    12 changes: 12 additions & 0 deletions messages.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    // The API accepts the page parameter and returns the pagination info in meta

    {
    "messages": [ ... ],
    "meta": {
    "pagination": {
    "total_pages": 3,
    "current_page": 1,
    "total_count": 55
    }
    }
    }
    4 changes: 1 addition & 3 deletions paginationstuff.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@


    /*
    This can be mixed into a route to provide pagination support.
    */
    @@ -19,7 +17,7 @@ App._PaginatedRouteMixin = Ember.Mixin.create({
    actions: {
    gotoPage: function(pageNum){
    var last = this.get('controller.content.pagination.total_pages') || 1;
    var num = Das.clamp(pageNum, 1, last);
    var num = Util.clamp(pageNum, 1, last);
    this.transitionTo(this.paginationRoute, num);
    },
    nextPage: function(){
    10 changes: 10 additions & 0 deletions router.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@

    App.Router.map(function(){

    this.resource('messages', { path: '/messages'}, function(){
    this.route('page', { path: ':num' });
    });

    });


  2. tcjr created this gist Oct 11, 2013.
    75 changes: 75 additions & 0 deletions paginationstuff.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@


    /*
    This can be mixed into a route to provide pagination support.
    */
    App._PaginatedRouteMixin = Ember.Mixin.create({
    paginationRoute: Ember.required(String),

    // This function is for use in a route that calls find() to get a
    // paginated collection of records. It takes the pagination metadata
    // from the store and puts it into the record array.
    _includePagination: function(records){
    var metadata = records.store.typeMapFor(records.type).metadata;
    // put the pagination content directly on the collection
    records.set('pagination', metadata.pagination);
    return records;
    },

    actions: {
    gotoPage: function(pageNum){
    var last = this.get('controller.content.pagination.total_pages') || 1;
    var num = Das.clamp(pageNum, 1, last);
    this.transitionTo(this.paginationRoute, num);
    },
    nextPage: function(){
    var cur = this.get('controller.content.pagination.current_page') || 0;
    this.transitionTo(this.paginationRoute, cur + 1);
    },
    previousPage: function(){
    var cur = this.get('controller.content.pagination.current_page') || 2;
    this.transitionTo(this.paginationRoute, cur - 1);
    },
    lastPage: function(){
    var last = this.get('controller.content.pagination.total_pages') || 1;
    this.transitionTo(this.paginationRoute, last);
    },
    firstPage: function(){
    this.transitionTo(this.paginationRoute, 1);
    }
    }

    });


    // MODEL

    App.Message = DS.Model.extend({
    created_at: DS.attr('date'),
    text: DS.attr('string')
    });


    // ROUTES

    App.MessagesIndexRoute = Ember.Route.extend({
    redirect: function(){
    this.transitionTo('messages.page', 1);
    }
    });

    App.MessagesPageRoute = Ember.Route.extend(App._PaginatedRouteMixin, {
    paginationRoute: 'messages.page',

    model: function(params){
    var query = { page: params.num };
    return this.get('store').find('message', query).then(this._includePagination);
    }

    });


    // CONTROLLER

    App.MessagesPageController = Ember.ArrayController.extend({
    });