diff --git a/addons/web/static/src/js/corelib.js b/addons/web/static/src/js/corelib.js index 5bbd5316521..bb74369989d 100644 --- a/addons/web/static/src/js/corelib.js +++ b/addons/web/static/src/js/corelib.js @@ -228,6 +228,38 @@ instance.web.ParentedMixin = { isDestroyed : function() { return this.__parentedDestroyed; }, + /** + Utility method to only execute asynchronous actions if the current + object has not been destroyed. + + @param {Promise} promise The promise representing the asynchronous action. + @param {bool} reject Defaults to false. If true, the returned promise will be + rejected with no arguments if the current object is destroyed. If false, + the returned promise will never be resolved nor rejected. + @returns {Promise} A promise that will mirror the given promise if everything goes + fine but will either be rejected with no arguments or never resolved if the + current object is destroyed. + */ + alive: function(promise, reject) { + var def = $.Deferred(); + var self = this; + promise.done(function() { + if (! self.isDestroyed()) { + if (! reject) + def.resolve.apply(def, arguments); + else + def.reject(); + } + }).fail(function() { + if (! self.isDestroyed()) { + if (! reject) + def.reject.apply(def, arguments); + else + def.reject(); + } + }); + return def.promise(); + }, /** * Inform the object it should destroy itself, releasing any * resource it could have reserved. @@ -495,16 +527,7 @@ instance.web.Controller = instance.web.Class.extend(instance.web.PropertiesMixin return false; }, rpc: function(url, data, options) { - var def = $.Deferred(); - var self = this; - instance.session.rpc(url, data, options).done(function() { - if (!self.isDestroyed()) - def.resolve.apply(def, arguments); - }).fail(function() { - if (!self.isDestroyed()) - def.reject.apply(def, arguments); - }); - return def.promise(); + return this.alive(instance.session.rpc(url, data, options)); } });