fixed saving settings. now done more generically.

This commit is contained in:
kay.one 2013-06-18 21:27:41 -07:00
parent d126c6efc1
commit 806e58ed1f
13 changed files with 104 additions and 161 deletions

View File

@ -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);
});

View File

@ -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++;
}
});
});

View File

@ -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);
});

View File

@ -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);
});

View File

@ -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);
});

View File

@ -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);
});

View File

@ -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);

View File

@ -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;
var promise = this.model.saveSettings();
this.model.save(undefined, SyncNotification.callback({
successMessage : success,
errorMessage : fail,
successCallback: this._saveSuccess,
context : this
}));
if (promise) {
promise.done(this._saveSuccess);
}
},
_deleteNotification: function () {

View File

@ -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"
}));
}
});
});

View File

@ -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);
});

View File

@ -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);
});

View File

@ -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);
}
}
};
}
};
});

View File

@ -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;
}
};
});