diff --git a/cms/static/js/models/settings/advanced.js b/cms/static/js/models/settings/advanced.js index 089c5bd22d..fe76164c61 100644 --- a/cms/static/js/models/settings/advanced.js +++ b/cms/static/js/models/settings/advanced.js @@ -16,5 +16,42 @@ CMS.Models.Settings.Advanced = Backbone.Model.extend({ } } if (!_.isEmpty(errors)) return errors; + }, + + save : function (attrs, options) { + // wraps the save call w/ the deletion of the removed keys after we know the saved ones worked + options = options ? _.clone(options) : {}; + // add saveSuccess to the success + var success = options.success; + var model = this; + options.success = function(model, resp, options) { + model.afterSave(model); + if (success) success(model, resp, options); + }; + Backbone.Model.prototype.save.call(this, attrs, options); + }, + + afterSave : function(self) { + // remove deleted attrs + if (!_.isEmpty(self.deleteKeys)) { + // remove the to be deleted keys from the returned model + _.each(self.deleteKeys, function(key) { self.unset(key); }); + // not able to do via backbone since we're not destroying the model + $.ajax({ + url : self.url, + // json to and fro + contentType : "application/json", + dataType : "json", + // delete + type : 'DELETE', + // data + data : JSON.stringify({ deleteKeys : self.deleteKeys}) + }) + .fail(function(hdr, status, error) { CMS.ServerError(self, "Deleting keys:" + status); }) + .done(function(data, status, error) { + // clear deleteKeys on success + self.deleteKeys = []; + }); + } } }); diff --git a/cms/static/js/views/settings/advanced_view.js b/cms/static/js/views/settings/advanced_view.js index d6484c88af..abf7bca9c4 100644 --- a/cms/static/js/views/settings/advanced_view.js +++ b/cms/static/js/views/settings/advanced_view.js @@ -73,35 +73,15 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ // TODO one last verification scan: // call validateKey on each to ensure proper format // check for dupes - + var self = this; this.model.save({}, { success : function() { + self.render(); window.alert("Saved"); }, error : CMS.ServerError }); - // FIXME don't delete if the validation didn't succeed in the save call - // remove deleted attrs - if (!_.isEmpty(this.model.deleteKeys)) { - var self = this; - // not able to do via backbone since we're not destroying the model - $.ajax({ - url : this.model.url, - // json to and fro - contentType : "application/json", - dataType : "json", - // delete - type : 'DELETE', - // data - data : JSON.stringify({ deleteKeys : this.model.deleteKeys}) - }) - .fail(function(hdr, status, error) { CMS.ServerError(self.model, "Deleting keys:" + status); }) - .done(function(data, status, error) { - // clear deleteKeys on success - self.model.deleteKeys = []; - }); - } }, revertView : function(event) { this.model.deleteKeys = []; @@ -149,6 +129,10 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ var validation = this.model.validate(newEntryModel); if (validation) { console.log('reserved key'); + if (_.has(validation, newKey)) { + // swap to the key which the map knows about + validation[oldKey] = validation[newKey]; + } this.model.trigger("error", this.model, validation); // abandon update return;