From 874e98b408cbeefa3481c9f22debf175a7dfbbc5 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 9 Dec 2013 18:40:20 -0800 Subject: [PATCH] Support for persistent state for collections --- src/UI/Missing/Collection.js | 10 ++-- src/UI/Mixins/AsPersistedStateCollection.js | 53 +++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/UI/Mixins/AsPersistedStateCollection.js diff --git a/src/UI/Missing/Collection.js b/src/UI/Missing/Collection.js index 4b7a4fa96..d58b6d133 100644 --- a/src/UI/Missing/Collection.js +++ b/src/UI/Missing/Collection.js @@ -2,11 +2,13 @@ define( [ 'Series/EpisodeModel', - 'backbone.pageable' - ], function (EpisodeModel, PagableCollection) { - return PagableCollection.extend({ + 'backbone.pageable', + 'Mixins/AsPersistedStateCollection' + ], function (EpisodeModel, PagableCollection, AsPersistedStateCollection) { + var collection = PagableCollection.extend({ url : window.NzbDrone.ApiRoot + '/missing', model: EpisodeModel, + tableName: 'missing', state: { pageSize: 15, @@ -38,4 +40,6 @@ define( return resp; } }); + + return AsPersistedStateCollection.call(collection); }); diff --git a/src/UI/Mixins/AsPersistedStateCollection.js b/src/UI/Mixins/AsPersistedStateCollection.js new file mode 100644 index 000000000..c74e3fcd5 --- /dev/null +++ b/src/UI/Mixins/AsPersistedStateCollection.js @@ -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; + }; + } +);