diff options
| author | Géry Debongnie <ged@odoo.com> | 2017-03-24 13:20:52 +0100 |
|---|---|---|
| committer | Aaron Bohy <aab@odoo.com> | 2017-04-11 19:44:38 +0200 |
| commit | 0250e60dd2d9038dc3f6a3306a470f8d3af87515 (patch) | |
| tree | 09a249e7336dc7a35a7e52e25bb7d4ceb59ee328 /addons/web/static/src/js/views | |
| parent | 6a8e7acc6783b440451728291f3dfcd5917b62e5 (diff) | |
[FIX] web: check if changes can be discarded before leaving view
Before this commit, we did not check if the changes can be discarded
when opening the app switcher. This is now done. Note that this check
was formerly done in the form view, and we moved this to the view
manager. This now means that all views have the power to decide if they
can be discarded.
Diffstat (limited to 'addons/web/static/src/js/views')
| -rw-r--r-- | addons/web/static/src/js/views/abstract_controller.js | 16 | ||||
| -rw-r--r-- | addons/web/static/src/js/views/form/form_controller.js | 55 |
2 files changed, 44 insertions, 27 deletions
diff --git a/addons/web/static/src/js/views/abstract_controller.js b/addons/web/static/src/js/views/abstract_controller.js index 3d9db03eb1c..9344d3a9e21 100644 --- a/addons/web/static/src/js/views/abstract_controller.js +++ b/addons/web/static/src/js/views/abstract_controller.js @@ -64,6 +64,22 @@ var AbstractController = Widget.extend({ //-------------------------------------------------------------------------- /** + * Determines if we can discard the current state. For example, when the + * user open the 'home' screen, the view manager will call this method on + * the active view to make sure it is ok to open the home screen (and + * potentially loose all current state). + * + * Note that it returns a deferred, because the view could choose to ask the + * user if he agrees to discard. + * + * @override + * @returns {Deferred} If the deferred is resolved, we assume the changes + * can be discarded. If it is rejected, then we cannot discard. + */ + canBeDiscarded: function () { + return $.when(); + }, + /** * Returns any special keys that may be useful when reloading the view to * get the same effect. This is necessary for saving the current view in * the favorites. For example, a graph view might want to add a key to diff --git a/addons/web/static/src/js/views/form/form_controller.js b/addons/web/static/src/js/views/form/form_controller.js index bf06c39034d..ff5510ae7e6 100644 --- a/addons/web/static/src/js/views/form/form_controller.js +++ b/addons/web/static/src/js/views/form/form_controller.js @@ -56,6 +56,32 @@ var FormController = BasicController.extend({ autofocus: function () { }, /** + * Determines if we can discard the current change. If the model is not + * dirty, that is not a problem. However, if it is dirty, we have to ask + * the user for confirmation. + * + * @override + * @returns {Deferred} If the deferred is resolved, we assume the changes + * can be discarded. If it is rejected, then we cannot discard. + */ + canBeDiscarded: function () { + if (!this.isDirty) { + return $.when(); + } + var message = _t("The record has been modified, your changes will be discarded. Are you sure you want to leave this page ?"); + var def = $.Deferred(); + var options = { + title: _t("Warning"), + confirm_callback: function () { + this.on('closed', null, def.resolve.bind(def)); + }, + cancel_callback: def.reject.bind(def) + }; + var dialog = Dialog.confirm(this, message, options); + dialog.$modal.on('hidden.bs.modal', def.reject.bind(def)); + return def; + }, + /** * @returns {boolean} */ checkInvalidFields: function () { @@ -146,7 +172,7 @@ var FormController = BasicController.extend({ */ renderPager: function ($node, options) { options = _.extend({}, options, { - validate: this._canBeDiscarded.bind(this), + validate: this.canBeDiscarded.bind(this), }); this._super($node, options); }, @@ -267,31 +293,6 @@ var FormController = BasicController.extend({ return def; }, /** - * Determines if we can discard the current change. If the model is not - * dirty, that is not a problem. However, if it is dirty, we have to ask - * the user for confirmation. - * - * @returns {Deferred} If the deferred is resolved, we assume the changes - * can be discarded. If it has been rejected, then we cannot discard. - */ - _canBeDiscarded: function () { - if (!this.isDirty) { - return $.when(); - } - var message = _t("The record has been modified, your changes will be discarded. Are you sure you want to leave this page ?"); - var def = $.Deferred(); - var options = { - title: _t("Warning"), - confirm_callback: function () { - this.on('closed', null, def.resolve.bind(def)); - }, - cancel_callback: def.reject.bind(def) - }; - var dialog = Dialog.confirm(this, message, options); - dialog.$modal.on('hidden.bs.modal', def.reject.bind(def)); - return def; - }, - /** * @override method from FieldManagerMixin * @param {string} id it is ignored, because we already know it. * @param {string[]} fields @@ -446,7 +447,7 @@ var FormController = BasicController.extend({ */ _onDiscardChange: function () { var self = this; - this._canBeDiscarded().then(function () { + this.canBeDiscarded().then(function () { self.model.discardChanges(self.handle); if (!self.model.isNew(self.handle)) { self._toReadOnlyMode(); |
