/* * This file is part of Phraseanet * * (c) 2005-2013 Alchemy * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ define([ "jquery", "underscore", "backbone", "i18n", "bootstrap", "apps/admin/fields/views/alert" ], function($, _, Backbone, i18n, bootstrap, AlertView) { var SaveView = Backbone.View.extend({ initialize: function() { var self = this; this.previousAttributes = []; this.$overlay = null; AdminFieldApp.errorManager.on("add-error", function(errors) { self._disableSaveButton(true); }); AdminFieldApp.errorManager.on("no-error", function() { self._disableSaveButton(false); }); }, events: { "click button.save-all" : "clickSaveAction" }, clickSaveAction: function(event) { var self = this; if (this._isModelDesync()) { this._loadingState(true); AdminFieldApp.fieldsCollection.save({ success: function(fields) { // reset collection with new one AdminFieldApp.fieldsCollection.reset(fields); new AlertView({ alert: "success", message: i18n.t("fields_save") }).render(); }, error: function(xhr, textStatus, errorThrown) { new AlertView({ alert: "error", message: '' !== xhr.responseText ? xhr.responseText : i18n.t("something_wrong") }).render(); } }).done(function() { self._loadingState(false); }); } return this; }, render: function () { var template = _.template($("#save_template").html()); this.$el.html(template); this.updateStateButton(); return this; }, updateStateButton: function() { this._disableSaveButton(!this._isModelDesync()); }, // check whether model has changed or not _isModelDesync: function () { return "undefined" !== typeof AdminFieldApp.fieldsCollection.find(function(model) { return !_.isEmpty(model.previousAttributes()); }); }, // create a transparent overlay on top of the application _overlay: function(showOrHide) { if(showOrHide && !this.$overlay) { this.$overlay = $("