Support for persistent state for collections

This commit is contained in:
Mark McDowall 2013-12-09 18:40:20 -08:00
parent cec479923f
commit 874e98b408
2 changed files with 60 additions and 3 deletions

View File

@ -2,11 +2,13 @@
define( define(
[ [
'Series/EpisodeModel', 'Series/EpisodeModel',
'backbone.pageable' 'backbone.pageable',
], function (EpisodeModel, PagableCollection) { 'Mixins/AsPersistedStateCollection'
return PagableCollection.extend({ ], function (EpisodeModel, PagableCollection, AsPersistedStateCollection) {
var collection = PagableCollection.extend({
url : window.NzbDrone.ApiRoot + '/missing', url : window.NzbDrone.ApiRoot + '/missing',
model: EpisodeModel, model: EpisodeModel,
tableName: 'missing',
state: { state: {
pageSize: 15, pageSize: 15,
@ -38,4 +40,6 @@ define(
return resp; return resp;
} }
}); });
return AsPersistedStateCollection.call(collection);
}); });

View File

@ -0,0 +1,53 @@
'use strict';
define(
['Config'],
function (Config) {
return function () {
var originalInit = this.prototype.initialize;
this.prototype.initialize = function () {
if (!this.tableName) {
throw 'tableName is required';
}
_setState.call(this);
this.on('backgrid:sort', _storeState, this);
if (originalInit) {
originalInit.call(this);
}
};
var _setState = function () {
var key = Config.getValue('{0}.sortKey'.format(this.tableName), this.state.sortKey);
var direction = Config.getValue('{0}.sortDirection'.format(this.tableName), this.state.order);
var order = parseInt(direction, 10);
this.state.sortKey = key;
this.state.order = order;
};
var _storeState = function (sortKey, sortDirection) {
var order = _convertDirectionToInt(sortDirection);
Config.setValue('{0}.sortKey'.format(this.tableName), sortKey);
Config.setValue('{0}.sortDirection'.format(this.tableName), order);
};
var _convertDirectionToInt = function (dir) {
if (dir === 'ascending') {
return '-1';
}
return '1';
};
return this;
};
}
);