1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00
Ghost-Admin/app/mixins/pagination.js
Kevin Ansfield 7eefbba69f 💄🐷 sort-imports eslint rule (#712)
no issue

- adds `eslint-plugin-sort-imports-es6-autofix` dependency
  - implements ESLint's base `sort-imports` rule but has a distinction in that `import {foo} from 'bar';` is considered `multiple` rather than `single`
  - fixes ESLint's autofix behaviour so `eslint --fix` will actually fix the sort order
- updates all unordered import rules by using `eslint --fix`

With the increased number of `import` statements since Ember+ecosystem started moving towards es6 modules I've found it frustrating at times trying to search through randomly ordered import statements. Recently I've been sorting imports manually when I've added new code or touched old code so I thought I'd add an ESLint rule to codify it.
2017-05-29 20:50:03 +02:00

111 lines
3.4 KiB
JavaScript

import Mixin from 'ember-metal/mixin';
import RSVP from 'rsvp';
import computed from 'ember-computed';
import injectService from 'ember-service/inject';
import {assign} from 'ember-platform';
let defaultPaginationSettings = {
page: 1,
limit: 15
};
export default Mixin.create({
notifications: injectService(),
paginationModel: null,
paginationSettings: 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() {
// don't merge defaults if paginationSettings is a CP
if (!this.paginationSettings.isDescriptor) {
let paginationSettings = this.get('paginationSettings');
let settings = assign({}, defaultPaginationSettings, paginationSettings);
this.set('paginationSettings', settings);
}
this.set('paginationMeta', {});
this._super(...arguments);
},
reportLoadError(error) {
this.get('notifications').showAPIError(error, {key: 'pagination.load.failed'});
},
loadFirstPage(transition) {
let paginationSettings = this.get('paginationSettings');
let modelName = this.get('paginationModel');
this.set('paginationSettings.page', 1);
this.set('isLoading', true);
return this.get('store').query(modelName, paginationSettings).then((results) => {
this.set('paginationMeta', results.meta);
return results;
}).catch((error) => {
// if we have a transition we're executing in a route hook so we
// want to throw in order to trigger the global error handler
if (transition) {
throw error;
} else {
this.reportLoadError(error);
}
}).finally(() => {
this.set('isLoading', false);
});
},
actions: {
loadFirstPage() {
return this.loadFirstPage();
},
/**
* Loads the next paginated page of posts into the ember-data store. Will cause the posts list UI to update.
* @return
*/
loadNextPage() {
let store = this.get('store');
let modelName = this.get('paginationModel');
let metadata = this.get('paginationMeta');
let nextPage = metadata.pagination && metadata.pagination.next;
let paginationSettings = this.get('paginationSettings');
if (nextPage && !this.get('isLoading')) {
this.set('isLoading', true);
this.set('paginationSettings.page', nextPage);
return store.query(modelName, paginationSettings).then((results) => {
this.set('paginationMeta', results.meta);
return results;
}).catch((error) => {
this.reportLoadError(error);
}).finally(() => {
this.set('isLoading', false);
});
} else {
return RSVP.resolve([]);
}
},
resetPagination() {
this.set('paginationSettings.page', 1);
}
}
});