From 806e58ed1f46f7208c53c648383895c01d1c6e95 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 18 Jun 2013 21:27:41 -0700 Subject: [PATCH] fixed saving settings. now done more generically. --- UI/Settings/General/GeneralView.js | 22 +++------------- UI/Settings/Indexers/CollectionView.js | 29 +-------------------- UI/Settings/Indexers/EditView.js | 24 ++--------------- UI/Settings/Indexers/ItemView.js | 6 +++-- UI/Settings/Indexers/Model.js | 10 ++++--- UI/Settings/Naming/NamingModel.js | 10 +++---- UI/Settings/Naming/NamingView.js | 14 ++-------- UI/Settings/Notifications/EditView.js | 18 +++++-------- UI/Settings/SettingsLayout.js | 25 ++---------------- UI/Settings/SettingsModel.js | 11 ++++---- UI/Settings/SettingsModelBase.js | 36 ++++++++++++++++++++++++++ UI/Settings/SyncNotification.js | 27 ------------------- UI/Shared/Messenger.js | 35 +++++++++++++++++++++++-- 13 files changed, 105 insertions(+), 162 deletions(-) create mode 100644 UI/Settings/SettingsModelBase.js delete mode 100644 UI/Settings/SyncNotification.js diff --git a/UI/Settings/General/GeneralView.js b/UI/Settings/General/GeneralView.js index 40ccf8867..aa208a0c5 100644 --- a/UI/Settings/General/GeneralView.js +++ b/UI/Settings/General/GeneralView.js @@ -1,24 +1,8 @@ 'use strict'; -define(['app', 'Mixins/AsModelBoundView'], function (App, AsModelBoundView) { - - var view = Backbone.Marionette.ItemView.extend({ - template: 'Settings/General/GeneralTemplate', - - initialize: function () { - NzbDrone.vent.on(NzbDrone.Commands.SaveSettings, this.saveSettings, this); - }, - - saveSettings: function () { - if (!this.model.isSaved) { - this.model.save(undefined, NzbDrone.Settings.SyncNotificaiton.callback({ - successMessage: 'General Settings saved', - errorMessage : "Failed to save General Settings" - })); - } - } +define(['marionette'], function (Marionette) { + return Marionette.ItemView.extend({ + template: 'Settings/General/GeneralTemplate' } ); - - return AsModelBoundView.call(view); }); diff --git a/UI/Settings/Indexers/CollectionView.js b/UI/Settings/Indexers/CollectionView.js index 72520d3c3..549168abe 100644 --- a/UI/Settings/Indexers/CollectionView.js +++ b/UI/Settings/Indexers/CollectionView.js @@ -1,11 +1,10 @@ 'use strict'; define(['app', 'marionette', - 'Shared/Messenger', 'Settings/Indexers/ItemView', 'Settings/Indexers/EditView', 'Settings/Indexers/Collection'], - function (App, Marionette, Messenger, IndexerItemView, IndexerEditView, IndexerCollection) { + function (App, Marionette, IndexerItemView, IndexerEditView, IndexerCollection) { return Marionette.CompositeView.extend({ itemView : IndexerItemView, itemViewContainer: '#x-indexers', @@ -15,10 +14,6 @@ define(['app', 'click .x-add': 'openSchemaModal' }, - initialize: function () { - this.listenTo(App.vent, App.Commands.SaveSettings, this._saveSettings); - this.savedCount = 0; - }, openSchemaModal: function () { var self = this; @@ -36,28 +31,6 @@ define(['app', App.modalRegion.show(view); } }); - }, - - _saveSettings: function () { - var self = this; - - _.each(this.collection.models, function (model, index, list) { - model.saveIfChanged(NzbDrone.Settings.SyncNotificaiton.callback({ - errorMessage : 'Failed to save indexer: ' + model.get('name'), - successCallback: self._saveSuccessful, - context : self - })); - }); - - if (self.savedCount > 0) { - Messenger.show({message: 'Indexer settings saved'}); - } - - this.savedCount = 0; - }, - - _saveSuccessful: function () { - this.savedCount++; } }); }); diff --git a/UI/Settings/Indexers/EditView.js b/UI/Settings/Indexers/EditView.js index 29cb641a2..7eaffbce9 100644 --- a/UI/Settings/Indexers/EditView.js +++ b/UI/Settings/Indexers/EditView.js @@ -3,10 +3,8 @@ define([ 'app', 'marionette', - 'Shared/Messenger', 'Mixins/AsModelBoundView' - -], function (App, Marionette, Messenger, AsModelBoundView) { +], function (App, Marionette, AsModelBoundView) { var view = Marionette.ItemView.extend({ template: 'Settings/Indexers/EditTemplate', @@ -20,27 +18,9 @@ define([ }, save: function () { - this.model.save(undefined, this.syncNotification("Indexer Saved", "Couldn't Save Indexer", this)); - }, - - syncNotification: function (success, error, context) { - return { - success: function () { - Messenger.show({ - message: success - }); - - context.indexerCollection.add(context.model); - App.modalRegion.closeModal(); - }, - - error: function () { - window.alert(error); - } - }; + this.model.saveSettings(); } }); return AsModelBoundView.call(view); - }); diff --git a/UI/Settings/Indexers/ItemView.js b/UI/Settings/Indexers/ItemView.js index 75ffd721b..70b537408 100644 --- a/UI/Settings/Indexers/ItemView.js +++ b/UI/Settings/Indexers/ItemView.js @@ -1,10 +1,12 @@ "use strict"; -define(['marionette'], function () { +define(['marionette', 'Mixins/AsModelBoundView'], function (Marionette, AsModelBoundView) { - return Marionette.ItemView.extend({ + var view = Marionette.ItemView.extend({ template: 'Settings/Indexers/ItemTemplate', tagName : 'li' }); + return AsModelBoundView.call(view); + }); diff --git a/UI/Settings/Indexers/Model.js b/UI/Settings/Indexers/Model.js index 1eab3e345..e54ea681b 100644 --- a/UI/Settings/Indexers/Model.js +++ b/UI/Settings/Indexers/Model.js @@ -1,9 +1,11 @@ "use strict"; define([ - 'backbone.deepmodel', 'Mixins/AsChangeTrackingModel'], function (DeepModel, AsChangeTrackingModel) { - var model = DeepModel.DeepModel.extend({ + 'Settings/SettingsModelBase'], function (ModelBase) { + return ModelBase.extend({ + + successMessage: 'Indexer Saved', + errorMessage : 'Couldn\'t save indexer' - }); - return AsChangeTrackingModel.call(model); + }); }); diff --git a/UI/Settings/Naming/NamingModel.js b/UI/Settings/Naming/NamingModel.js index 0f9962c17..25b7b933a 100644 --- a/UI/Settings/Naming/NamingModel.js +++ b/UI/Settings/Naming/NamingModel.js @@ -1,10 +1,10 @@ "use strict"; define(['app', - 'Mixins/AsChangeTrackingModel'], function (App, AsChangeTrackingModel) { - var model = Backbone.Model.extend({ - url: App.Constants.ApiRoot + '/config/naming' + 'Settings/SettingsModelBase'], function (App, ModelBase) { + return ModelBase.extend({ + url : App.Constants.ApiRoot + '/config/naming', + successMessage: 'Naming settings saved', + errorMessage : 'Couldn\'t save naming settings' }); - return AsChangeTrackingModel.call(model); - }); diff --git a/UI/Settings/Naming/NamingView.js b/UI/Settings/Naming/NamingView.js index 93ab4bfed..a360f5869 100644 --- a/UI/Settings/Naming/NamingView.js +++ b/UI/Settings/Naming/NamingView.js @@ -2,8 +2,7 @@ define(['app', 'marionette', 'Settings/Naming/NamingModel', - 'Settings/SyncNotification', - 'Mixins/AsModelBoundView'], function (App, Marionette, NamingModel, SyncNotification, AsModelBoundView) { + 'Mixins/AsModelBoundView'], function (App, Marionette, NamingModel, AsModelBoundView) { var view = Marionette.ItemView.extend({ template: 'Settings/Naming/NamingTemplate', @@ -11,17 +10,8 @@ define(['app', initialize: function () { this.model = new NamingModel(); this.model.fetch(); - - this.listenTo(App.vent, App.Commands.SaveSettings, this.saveSettings); - - }, - - saveSettings: function () { - this.model.saveIfChanged(undefined, SyncNotification.callback({ - successMessage: 'Naming Settings saved', - errorMessage : "Failed to save Naming Settings" - })); } + }); return AsModelBoundView.call(view); diff --git a/UI/Settings/Notifications/EditView.js b/UI/Settings/Notifications/EditView.js index 09ea025fb..f20e4eb61 100644 --- a/UI/Settings/Notifications/EditView.js +++ b/UI/Settings/Notifications/EditView.js @@ -5,11 +5,10 @@ define([ 'marionette', 'Settings/Notifications/Model', 'Settings/Notifications/DeleteView', - 'Settings/SyncNotification', 'Shared/Messenger', 'Mixins/AsModelBoundView' -], function (App, Marionette, NotificationModel, DeleteView, SyncNotification, Messenger, AsModelBoundView) { +], function (App, Marionette, NotificationModel, DeleteView, Messenger, AsModelBoundView) { var model = Marionette.ItemView.extend({ template: 'Settings/Notifications/EditTemplate', @@ -30,16 +29,11 @@ define([ }, _saveNotification: function () { - var name = this.model.get('name'); - var success = 'Notification Saved: ' + name; - var fail = 'Failed to save notification: ' + name; - - this.model.save(undefined, SyncNotification.callback({ - successMessage : success, - errorMessage : fail, - successCallback: this._saveSuccess, - context : this - })); + var promise = this.model.saveSettings(); + + if (promise) { + promise.done(this._saveSuccess); + } }, _deleteNotification: function () { diff --git a/UI/Settings/SettingsLayout.js b/UI/Settings/SettingsLayout.js index 3bc37dec9..c9dc9aa25 100644 --- a/UI/Settings/SettingsLayout.js +++ b/UI/Settings/SettingsLayout.js @@ -13,24 +13,9 @@ define([ 'Settings/Notifications/CollectionView', 'Settings/Notifications/Collection', 'Settings/General/GeneralView', - 'Settings/Misc/MiscView', - 'Settings/SyncNotification' + 'Settings/Misc/MiscView' ], - function (App, - Marionette, - SettingsModel, - GeneralSettingsModel, - NamingView, - NamingModel, - QualityLayout, - IndexerCollectionView, - IndexerCollection, - DownloadClientView, - NotificationCollectionView, - NotificationCollection, - GeneralView, - MiscView, - SyncNotification) { + function (App, Marionette, SettingsModel, GeneralSettingsModel, NamingView, NamingModel, QualityLayout, IndexerCollectionView, IndexerCollection, DownloadClientView, NotificationCollectionView, NotificationCollection, GeneralView, MiscView) { return Marionette.Layout.extend({ template: 'Settings/SettingsLayoutTemplate', @@ -185,13 +170,7 @@ define([ }, save: function () { - App.vent.trigger(App.Commands.SaveSettings); - - this.settings.saveIfChanged(undefined, SyncNotification.callback({ - successMessage: 'Settings saved', - errorMessage : "Failed to save settings" - })); } }); }); diff --git a/UI/Settings/SettingsModel.js b/UI/Settings/SettingsModel.js index 19d097275..19a12e57c 100644 --- a/UI/Settings/SettingsModel.js +++ b/UI/Settings/SettingsModel.js @@ -1,10 +1,9 @@ "use strict"; define(['app', - 'backbone', - 'Mixins/SaveIfChangedModel'], function (App, Backbone, AsChangeTrackingModel) { - var model = Backbone.Model.extend({ - url: App.Constants.ApiRoot + '/settings' + 'Settings/SettingsModelBase'], function (App, SettingsModelBase) { + return SettingsModelBase.extend({ + url : App.Constants.ApiRoot + '/settings', + successMessage: 'Settings saved', + errorMessage : "Failed to save settings" }); - - return AsChangeTrackingModel.call(model); }); diff --git a/UI/Settings/SettingsModelBase.js b/UI/Settings/SettingsModelBase.js new file mode 100644 index 000000000..78d8ebc8c --- /dev/null +++ b/UI/Settings/SettingsModelBase.js @@ -0,0 +1,36 @@ +"use strict"; +define(['app', + 'backbone.deepmodel', + 'Mixins/AsChangeTrackingModel', + 'Shared/Messenger'], function (App, DeepModel, AsChangeTrackingModel, Messenger) { + var model = DeepModel.DeepModel.extend({ + + initialize: function () { + + // App.vent.on(App.Commands.SaveSettings, this.saveSettings, this); + this.listenTo(App.vent, App.Commands.SaveSettings, this.saveSettings); + }, + + saveSettings: function () { + + if (!this.isSaved) { + + var savePromise = this.save(); + + Messenger.monitor( + { + promise : savePromise, + successMessage: this.successMessage, + errorMessage : this.errorMessage + }); + + return savePromise; + } + + return undefined; + } + + }); + + return AsChangeTrackingModel.call(model); +}); diff --git a/UI/Settings/SyncNotification.js b/UI/Settings/SyncNotification.js deleted file mode 100644 index b722e6582..000000000 --- a/UI/Settings/SyncNotification.js +++ /dev/null @@ -1,27 +0,0 @@ -"use strict"; -define(['shared/messenger'], function (Messenger) { - return { - callback: function (options) { - return { - success: function () { - if (options.successMessage) { - Messenger.show({message: options.successMessage}); - } - - if (options.successCallback) { - options.successCallback.call(options.context); - } - }, - error : function () { - if (options.errorMessage) { - Messenger.show({message: options.errorMessage, type: 'error'}); - } - - if (options.errorCallback) { - options.errorCallback.call(options.context); - } - } - }; - } - }; -}); diff --git a/UI/Shared/Messenger.js b/UI/Shared/Messenger.js index da850085f..bdda55a76 100644 --- a/UI/Shared/Messenger.js +++ b/UI/Shared/Messenger.js @@ -12,7 +12,8 @@ define(function () { case 'info': options.hideAfter = 5; break; - case 'error': + + default : options.hideAfter = 0; } } @@ -23,5 +24,35 @@ define(function () { showCloseButton: true, hideAfter : options.hideAfter }); - }}; + }, + + + monitor: function (options) { + + if (!options.promise) { + throw 'promise is required'; + } + + if (!options.successMessage) { + throw 'success message is required'; + } + + if (!options.errorMessage) { + throw 'error message is required'; + } + + var self = this; + + options.promise.done(function () { + self.show({message: options.successMessage}); + }); + + options.promise.fail(function () { + self.show({message: options.errorMessage, type: 'error'}); + }); + + return options.promise; + } + }; }); +