2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2015-05-27 03:41:12 +03:00
|
|
|
import getRequestErrorMessage from 'ghost/utils/ajax';
|
2014-07-20 20:42:03 +04:00
|
|
|
|
2015-05-27 03:41:12 +03:00
|
|
|
export default Ember.Mixin.create({
|
2015-05-26 05:10:50 +03:00
|
|
|
notifications: Ember.inject.service(),
|
|
|
|
|
2014-07-20 20:42:03 +04:00
|
|
|
// 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 += '.';
|
|
|
|
}
|
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
this.get('notifications').showError(message);
|
2014-07-20 20:42:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
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'),
|
2014-12-18 21:39:11 +03:00
|
|
|
metadata = this.store.metadataFor(recordType),
|
|
|
|
nextPage = metadata.pagination && metadata.pagination.next,
|
2014-07-20 20:42:03 +04:00
|
|
|
paginationSettings = this.get('paginationSettings');
|
|
|
|
|
|
|
|
if (nextPage) {
|
|
|
|
this.set('isLoading', true);
|
|
|
|
this.set('paginationSettings.page', nextPage);
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-07-20 20:42:03 +04:00
|
|
|
store.find(recordType, paginationSettings).then(function () {
|
|
|
|
self.set('isLoading', false);
|
|
|
|
}, function (response) {
|
|
|
|
self.reportLoadError(response);
|
|
|
|
});
|
|
|
|
}
|
2014-12-18 21:39:11 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
resetPagination: function () {
|
|
|
|
this.set('paginationSettings.page', 1);
|
2015-04-09 16:56:08 +03:00
|
|
|
this.store.setMetadataFor('tag', {pagination: undefined});
|
2014-07-20 20:42:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|