Last active
December 25, 2015 06:59
Revisions
-
tcjr revised this gist
Oct 11, 2013 . 3 changed files with 23 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal 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 } } } 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 charactersOriginal 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 = Util.clamp(pageNum, 1, last); this.transitionTo(this.paginationRoute, num); }, nextPage: function(){ 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 charactersOriginal 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' }); }); }); -
tcjr created this gist
Oct 11, 2013 .There are no files selected for viewing
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 charactersOriginal 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({ });