Skip to content

Commit

Permalink
Improve PaginationRoute mixin
Browse files Browse the repository at this point in the history
no issue
- returns the promise/result from `loadNextPage` so that it's return value can be utilised in closure actions
- sets the `isLoading` property in `loadFirstPage` to match `loadNextPage` behaviour
- reset the `isLoading` property even if the request fails
- adds a `didReceivePaginationMeta` hook so that consumers of the mixin can use the metadata values without having to rely on observers
  - eg. pulling the `total` into a separate property that can be manipulated when items are added/removed but still reset to the sever's total value the next time a page is loaded
- renames the `pagination-route` mixin to simply `pagination` as it's not tied to routes and works equally well in other objects that need to paginate an API resource
  • Loading branch information
kevinansfield committed Apr 19, 2016
1 parent 7cb03f8 commit 459f2c4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import getRequestErrorMessage from 'ghost/utils/ajax';

const {
Mixin,
computed,
inject: {service}
} = Ember;

Expand All @@ -16,7 +17,20 @@ export default Mixin.create({

paginationModel: null,
paginationSettings: null,
paginationMeta: null,

// add a hook so that routes/controllers can do something with the meta data
paginationMeta: computed({
get() {
return this._paginationMeta;
},
set(key, value) {
if (this.didReceivePaginationMeta) {
this.didReceivePaginationMeta(value);
}
this._paginationMeta = value;
return value;
}
}),

init() {
let paginationSettings = this.get('paginationSettings');
Expand Down Expand Up @@ -51,11 +65,15 @@ export default Mixin.create({

paginationSettings.page = 1;

this.set('isLoading', true);

return this.get('store').query(modelName, paginationSettings).then((results) => {
this.set('paginationMeta', results.meta);
return results;
}, (response) => {
}).catch((response) => {
this.reportLoadError(response);
}).finally(() => {
this.set('isLoading', false);
});
},

Expand All @@ -79,12 +97,13 @@ export default Mixin.create({
this.set('isLoading', true);
this.set('paginationSettings.page', nextPage);

store.query(modelName, paginationSettings).then((results) => {
this.set('isLoading', false);
return store.query(modelName, paginationSettings).then((results) => {
this.set('paginationMeta', results.meta);
return results;
}, (response) => {
}).catch((response) => {
this.reportLoadError(response);
}).finally(() => {
this.set('isLoading', false);
});
}
},
Expand Down
4 changes: 2 additions & 2 deletions core/client/app/routes/posts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Ember from 'ember';
import AuthenticatedRoute from 'ghost/routes/authenticated';
import ShortcutsRoute from 'ghost/mixins/shortcuts-route';
import PaginationRouteMixin from 'ghost/mixins/pagination-route';
import PaginationMixin from 'ghost/mixins/pagination';

export default AuthenticatedRoute.extend(ShortcutsRoute, PaginationRouteMixin, {
export default AuthenticatedRoute.extend(ShortcutsRoute, PaginationMixin, {
titleToken: 'Content',

paginationModel: 'post',
Expand Down
4 changes: 2 additions & 2 deletions core/client/app/routes/settings/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Ember from 'ember';
import AuthenticatedRoute from 'ghost/routes/authenticated';
import CurrentUserSettings from 'ghost/mixins/current-user-settings';
import ShortcutsRoute from 'ghost/mixins/shortcuts-route';
import PaginationRoute from 'ghost/mixins/pagination-route';
import PaginationMixin from 'ghost/mixins/pagination';

export default AuthenticatedRoute.extend(CurrentUserSettings, PaginationRoute, ShortcutsRoute, {
export default AuthenticatedRoute.extend(CurrentUserSettings, PaginationMixin, ShortcutsRoute, {
titleToken: 'Settings - Tags',

paginationModel: 'tag',
Expand Down
4 changes: 2 additions & 2 deletions core/client/app/routes/team/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import AuthenticatedRoute from 'ghost/routes/authenticated';
import CurrentUserSettings from 'ghost/mixins/current-user-settings';
import PaginationRouteMixin from 'ghost/mixins/pagination-route';
import PaginationMixin from 'ghost/mixins/pagination';
import styleBody from 'ghost/mixins/style-body';

export default AuthenticatedRoute.extend(styleBody, CurrentUserSettings, PaginationRouteMixin, {
export default AuthenticatedRoute.extend(styleBody, CurrentUserSettings, PaginationMixin, {
titleToken: 'Team',

classNames: ['view-team'],
Expand Down

0 comments on commit 459f2c4

Please sign in to comment.