From c94595b3ae44167a3ef8d684e1d7838ea2a10308 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 30 May 2013 21:06:52 -0700 Subject: [PATCH] SaveIfChangedMixin --- UI/Mixins/SaveIfChangedModel.js | 30 ++++++++++++++++++++++++++ UI/Settings/Indexers/CollectionView.js | 15 +++++++------ UI/Settings/Indexers/Model.js | 4 +++- UI/app.js | 1 + 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 UI/Mixins/SaveIfChangedModel.js diff --git a/UI/Mixins/SaveIfChangedModel.js b/UI/Mixins/SaveIfChangedModel.js new file mode 100644 index 000000000..d8475fda1 --- /dev/null +++ b/UI/Mixins/SaveIfChangedModel.js @@ -0,0 +1,30 @@ +//try to add ajax data as query string to DELETE calls. +"use strict"; +(function () { + + NzbDrone.Mixins.SaveIfChangedModel = { +// originalInitialize: this.initialize, + + initialize: function () { + this.isSaved = true; + + this.on('change', function () { + this.isSaved = false; + }, this); + + this.on('sync', function () { + this.isSaved = true; + }, this); + +// if (originalInitialize) { +// originalInitialize.call(this); +// } + }, + + saveIfChanged: function (options) { + if (!this.isSaved) { + this.save(undefined, options); + } + } + }; +}()); \ No newline at end of file diff --git a/UI/Settings/Indexers/CollectionView.js b/UI/Settings/Indexers/CollectionView.js index c16aaf163..89b223f44 100644 --- a/UI/Settings/Indexers/CollectionView.js +++ b/UI/Settings/Indexers/CollectionView.js @@ -36,23 +36,26 @@ define(['app', saveSettings: function () { var self = this; - - //For now loop through and save all the models - + _.each(this.collection.models, function (model, index, list) { var name = model.get('name'); var error = 'Failed to save indexer: ' + name; - model.save(undefined, self.syncNotification(error)); + model.saveIfChanged(self.syncNotification(undefined, error)); }); }, - syncNotification: function (error) { + syncNotification: function (success, error) { return { success: function () { + if (success) { + NzbDrone.Shared.Messenger.show({message: success}); + } }, error : function () { - NzbDrone.Shared.Messenger.show({message: "Couldn't Save General Settings", type: 'error'}); + if (error) { + NzbDrone.Shared.Messenger.show({message: error, type: 'error'}); + } } }; } diff --git a/UI/Settings/Indexers/Model.js b/UI/Settings/Indexers/Model.js index aa8592e0d..ba12b5537 100644 --- a/UI/Settings/Indexers/Model.js +++ b/UI/Settings/Indexers/Model.js @@ -1,5 +1,7 @@ "use strict"; -define(['app'], function () { +define(['app', 'Mixins/SaveIfChangedModel'], function () { NzbDrone.Settings.Indexers.Model = Backbone.DeepModel.extend({ }); + + _.extend(NzbDrone.Settings.Indexers.Model.prototype, NzbDrone.Mixins.SaveIfChangedModel); }); diff --git a/UI/app.js b/UI/app.js index 15248cc9a..4c20bf44f 100644 --- a/UI/app.js +++ b/UI/app.js @@ -82,6 +82,7 @@ define('app', ['shared/modal/region'], function (ModalRegion) { window.NzbDrone.Missing = {}; window.NzbDrone.History = {}; + window.NzbDrone.Mixins = {}; window.NzbDrone.Events = { SeriesAdded: 'seriesAdded'