0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

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 7cb03f8c91
commit 459f2c4355
4 changed files with 30 additions and 11 deletions

View file

@ -3,6 +3,7 @@ import getRequestErrorMessage from 'ghost/utils/ajax';
const { const {
Mixin, Mixin,
computed,
inject: {service} inject: {service}
} = Ember; } = Ember;
@ -16,7 +17,20 @@ export default Mixin.create({
paginationModel: null, paginationModel: null,
paginationSettings: 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() { init() {
let paginationSettings = this.get('paginationSettings'); let paginationSettings = this.get('paginationSettings');
@ -51,11 +65,15 @@ export default Mixin.create({
paginationSettings.page = 1; paginationSettings.page = 1;
this.set('isLoading', true);
return this.get('store').query(modelName, paginationSettings).then((results) => { return this.get('store').query(modelName, paginationSettings).then((results) => {
this.set('paginationMeta', results.meta); this.set('paginationMeta', results.meta);
return results; return results;
}, (response) => { }).catch((response) => {
this.reportLoadError(response); this.reportLoadError(response);
}).finally(() => {
this.set('isLoading', false);
}); });
}, },
@ -79,12 +97,13 @@ export default Mixin.create({
this.set('isLoading', true); this.set('isLoading', true);
this.set('paginationSettings.page', nextPage); this.set('paginationSettings.page', nextPage);
store.query(modelName, paginationSettings).then((results) => { return store.query(modelName, paginationSettings).then((results) => {
this.set('isLoading', false);
this.set('paginationMeta', results.meta); this.set('paginationMeta', results.meta);
return results; return results;
}, (response) => { }).catch((response) => {
this.reportLoadError(response); this.reportLoadError(response);
}).finally(() => {
this.set('isLoading', false);
}); });
} }
}, },

View file

@ -1,9 +1,9 @@
import Ember from 'ember'; import Ember from 'ember';
import AuthenticatedRoute from 'ghost/routes/authenticated'; import AuthenticatedRoute from 'ghost/routes/authenticated';
import ShortcutsRoute from 'ghost/mixins/shortcuts-route'; 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', titleToken: 'Content',
paginationModel: 'post', paginationModel: 'post',

View file

@ -3,9 +3,9 @@ import Ember from 'ember';
import AuthenticatedRoute from 'ghost/routes/authenticated'; import AuthenticatedRoute from 'ghost/routes/authenticated';
import CurrentUserSettings from 'ghost/mixins/current-user-settings'; import CurrentUserSettings from 'ghost/mixins/current-user-settings';
import ShortcutsRoute from 'ghost/mixins/shortcuts-route'; 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', titleToken: 'Settings - Tags',
paginationModel: 'tag', paginationModel: 'tag',

View file

@ -1,9 +1,9 @@
import AuthenticatedRoute from 'ghost/routes/authenticated'; import AuthenticatedRoute from 'ghost/routes/authenticated';
import CurrentUserSettings from 'ghost/mixins/current-user-settings'; 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'; import styleBody from 'ghost/mixins/style-body';
export default AuthenticatedRoute.extend(styleBody, CurrentUserSettings, PaginationRouteMixin, { export default AuthenticatedRoute.extend(styleBody, CurrentUserSettings, PaginationMixin, {
titleToken: 'Team', titleToken: 'Team',
classNames: ['view-team'], classNames: ['view-team'],