Improve PaginationRoute mixin

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
This commit is contained in:
Kevin Ansfield 2016-04-18 17:30:35 +01:00
parent cce23ca6e9
commit 5f7bd12eec
4 changed files with 30 additions and 11 deletions

View File

@ -3,6 +3,7 @@ import getRequestErrorMessage from 'ghost/utils/ajax';
const {
Mixin,
computed,
inject: {service}
} = Ember;
@ -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');
@ -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);
});
},
@ -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);
});
}
},

View File

@ -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',

View File

@ -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',

View File

@ -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'],