[FIX] web: Form: protect against widget destruction

When a button is clicked in a form view, the event 'execute_action'
is triggered up and catched by the view_manager, which can do
various async stuff. Meanwhile, the form view might be destroyed,
and in this case, the handlers shouldn't be executed when the
async stuff is done.
This commit is contained in:
Aaron Bohy
2017-04-11 19:44:38 +02:00
parent 3ff7ff3e94
commit c5cf445462
2 changed files with 29 additions and 3 deletions
@@ -266,6 +266,11 @@ var FormController = BasicController.extend({
_callButtonAction: function (attrs, record) {
var self = this;
var def = $.Deferred();
var reload = function () {
if (!self.isDestroyed()) {
self.reload();
}
};
record = record || this.model.get(this.handle);
var recordID = record.data.id;
this.trigger_up('execute_action', {
@@ -276,13 +281,13 @@ var FormController = BasicController.extend({
record_id: recordID,
on_closed: function (reason) {
if (!_.isObject(reason)) {
return self.reload();
reload();
}
},
on_fail: this.reload.bind(this),
on_fail: reload,
on_success: def.resolve.bind(def),
});
return def;
return this.alive(def);
},
/**
* @override method from FieldManagerMixin
@@ -2161,6 +2161,27 @@ QUnit.module('Views', {
}
});
QUnit.test('rpc complete after destroying parent', function (assert) {
// We just test that there is no crash in this situation
assert.expect(0);
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch: '<form>' +
'<button name="update_module" type="object" class="o_form_button_update"/>' +
'</form>',
res_id: 2,
intercepts: {
execute_action: function (event) {
form.destroy();
event.data.on_success();
}
}
});
form.$('.o_form_button_update').click();
});
QUnit.test('onchanges that complete after discarding', function (assert) {
assert.expect(4);