1
0
mirror of https://github.com/TryGhost/Ghost.git synced 2024-12-15 11:34:24 +03:00
Ghost/core/client/app/mixins/pagination-controller.js
Jason Williams c3ad1ae9e2 Use Ember.inject instead of needs and initializers
No Issue
- Switches to the newer style of dependency injection.
- Instead of injection Controllers via "needs," use
  Ember.inject.controller().
- Get rid of initializers that were only injecting objects
  into various factories. Converts these objects into Ember.Service
  objects and declaratively inject them where needed via
  Ember.inject.service().  The added benefit to this is that it's no
  longer a mystery where these properties/methods come from and it's
  straightforward to inject them where needed.
2015-05-27 07:41:42 -05:00

62 lines
2.0 KiB
JavaScript

import Ember from 'ember';
import getRequestErrorMessage from 'ghost/utils/ajax';
export default Ember.Mixin.create({
notifications: Ember.inject.service(),
// set from PaginationRouteMixin
paginationSettings: null,
// indicates whether we're currently loading the next page
isLoading: null,
/**
* Takes an ajax response, concatenates any error messages, then generates an error notification.
* @param {jqXHR} response The jQuery ajax reponse object.
* @return
*/
reportLoadError: function (response) {
var message = 'A problem was encountered while loading more records';
if (response) {
// Get message from response
message += ': ' + getRequestErrorMessage(response, true);
} else {
message += '.';
}
this.get('notifications').showError(message);
},
actions: {
/**
* Loads the next paginated page of posts into the ember-data store. Will cause the posts list UI to update.
* @return
*/
loadNextPage: function () {
var self = this,
store = this.get('store'),
recordType = this.get('model').get('type'),
metadata = this.store.metadataFor(recordType),
nextPage = metadata.pagination && metadata.pagination.next,
paginationSettings = this.get('paginationSettings');
if (nextPage) {
this.set('isLoading', true);
this.set('paginationSettings.page', nextPage);
store.find(recordType, paginationSettings).then(function () {
self.set('isLoading', false);
}, function (response) {
self.reportLoadError(response);
});
}
},
resetPagination: function () {
this.set('paginationSettings.page', 1);
this.store.setMetadataFor('tag', {pagination: undefined});
}
}
});