[IMP] Added utility method to better handle asynchronous calls and destruction of objects.
Critical to help solve many bugs. bzr revid: nicolas.vanhoren@openerp.com-20130227111619-dwhjt84tz8wpd6vp
This commit is contained in:
parent
650ff2dd5e
commit
1b4337f8e6
|
@ -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));
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue