mirror of
https://github.com/lidarr/Lidarr
synced 2024-12-24 16:51:58 +00:00
Merge pull request #690 from Sonarr/backgrid-pageable-reset
Alter backbone.pageable to stay on same page during fetch
This commit is contained in:
commit
fcfbfae4c1
2 changed files with 48 additions and 1 deletions
|
@ -1,8 +1,10 @@
|
|||
var _ = require('underscore');
|
||||
var PageableCollection = require('backbone.pageable');
|
||||
//var PageableCollection = require('../../Shared/Grid/SonarrPageableCollection');
|
||||
var QueueModel = require('./QueueModel');
|
||||
var FormatHelpers = require('../../Shared/FormatHelpers');
|
||||
var AsSortedCollection = require('../../Mixins/AsSortedCollection');
|
||||
var AsPageableCollection = require('../../Mixins/AsPageableCollection');
|
||||
var moment = require('moment');
|
||||
|
||||
require('../../Mixins/backbone.signalr.mixin');
|
||||
|
@ -64,9 +66,9 @@ var QueueCollection = PageableCollection.extend({
|
|||
});
|
||||
|
||||
QueueCollection = AsSortedCollection.call(QueueCollection);
|
||||
QueueCollection = AsPageableCollection.call(QueueCollection);
|
||||
|
||||
var collection = new QueueCollection().bindSignalR();
|
||||
collection.fetch();
|
||||
|
||||
|
||||
module.exports = collection;
|
45
src/UI/Mixins/AsPageableCollection.js
Normal file
45
src/UI/Mixins/AsPageableCollection.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
var _ = require('underscore');
|
||||
|
||||
module.exports = function() {
|
||||
var originalMakeCollectionEventHandler = this.prototype._makeCollectionEventHandler;
|
||||
|
||||
this.prototype._makeCollectionEventHandler = function (pageCollection, fullCollection) {
|
||||
var self = this;
|
||||
this.pageCollection = pageCollection;
|
||||
this.fullCollection = fullCollection;
|
||||
var eventHandler = originalMakeCollectionEventHandler.apply(this, arguments);
|
||||
|
||||
return _.wrap(eventHandler, _.bind(self._resetEventHandler, self));
|
||||
};
|
||||
|
||||
this.prototype._resetEventHandler = function (originalEventHandler, event, model, collection, options) {
|
||||
if (event === 'reset') {
|
||||
var currentPage = this.state.currentPage;
|
||||
var pageSize = this.state.pageSize;
|
||||
|
||||
originalEventHandler.apply(this, [].slice.call(arguments, 1));
|
||||
|
||||
var totalPages = Math.ceil(this.state.totalRecords / pageSize);
|
||||
var newPage = Math.min(currentPage, totalPages);
|
||||
|
||||
if (newPage !== this.state.currentPage) {
|
||||
this.state.currentPage = newPage;
|
||||
|
||||
// If backbone pageable fixes their reset bug
|
||||
// (they reset the page number, but not the range),
|
||||
// we'll want to do this for all resets where the page number changed
|
||||
if (currentPage !== newPage) {
|
||||
var pageStart = (newPage - 1) * pageSize;
|
||||
var pageEnd = pageStart + pageSize;
|
||||
|
||||
this.pageCollection.reset(this.fullCollection.models.slice(pageStart, pageEnd),
|
||||
_.extend({}, options, { parse : false }));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
originalEventHandler.call(this, [].slice.call(arguments, 1));
|
||||
}
|
||||
};
|
||||
|
||||
return this;
|
||||
};
|
Loading…
Reference in a new issue