[FIX] web: reload list view after button action

Before this commit, the code that handled buttons in list view was kind
of simple, and did not reload the view after.

But clicking on action button is not only a problem for the list view,
the form view already had something in the controller doing exactly
that.  With this commit, we move that code to the basic controller, and
use the method in the list view.
This commit is contained in:
Géry Debongnie
2017-04-24 10:43:20 +02:00
parent e59c1b49c2
commit a765ff503d
4 changed files with 45 additions and 41 deletions
@@ -87,6 +87,41 @@ return AbstractController.extend(FieldManagerMixin, {
// Private
//--------------------------------------------------------------------------
/**
* When the user clicks on a 'action button', this function determines what
* should happen.
*
* @private
* @param {Object} attrs the attrs of the button clicked
* @param {Object} [record] the current state of the view
* @returns {Deferred}
*/
_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', {
action_data: _.extend({}, attrs, {
context: record.getContext({additionalContext: attrs.context}),
}),
model: record.model,
record_id: recordID,
on_closed: function (reason) {
if (!_.isObject(reason)) {
reload();
}
},
on_fail: reload,
on_success: def.resolve.bind(def),
});
return this.alive(def);
},
/**
* Called by the field manager mixin to confirm that a change just occured
* (after that potential onchanges have been applied).
@@ -260,41 +260,6 @@ var FormController = BasicController.extend({
// Private
//--------------------------------------------------------------------------
/**
* When the user clicks on a form button, this function determines what
* should happen.
*
* @private
* @param {Object} attrs the attrs of the button clicked
* @param {Object} [record] the current state of the view
* @returns {Deferred}
*/
_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', {
action_data: _.extend({}, attrs, {
context: record.getContext({additionalContext: attrs.context}),
}),
model: record.model,
record_id: recordID,
on_closed: function (reason) {
if (!_.isObject(reason)) {
reload();
}
},
on_fail: reload,
on_success: def.resolve.bind(def),
});
return this.alive(def);
},
/**
* When a save operation has been confirmed from the model, this method is
* called.
@@ -210,11 +210,8 @@ var ListController = BasicController.extend({
* @param {OdooEvent} event
*/
_onButtonClicked: function (event) {
this.trigger_up('execute_action', {
action_data: event.data.attrs,
model: this.modelName,
record_id: event.data.record.res_id,
});
event.stopPropagation();
this._callButtonAction(event.data.attrs, event.data.record);
},
/**
* This event is triggered when a list renderer goes from mode = readonly to
+8 -1
View File
@@ -969,7 +969,7 @@ QUnit.module('Views', {
});
QUnit.test('click on a button in a list view', function (assert) {
assert.expect(6);
assert.expect(9);
var list = createView({
View: ListView,
@@ -979,6 +979,10 @@ QUnit.module('Views', {
'<field name="foo"/>' +
'<button string="a button" name="button_action" icon="fa-car" type="object"/>' +
'</tree>',
mockRPC: function (route) {
assert.step(route);
return this._super.apply(this, arguments);
},
intercepts: {
execute_action: function (event) {
assert.strictEqual(event.data.record_id, 1,
@@ -989,6 +993,7 @@ QUnit.module('Views', {
"should call correct method");
assert.strictEqual(event.data.action_data.type, 'object',
'should have correct type');
event.data.on_closed();
},
},
});
@@ -999,6 +1004,8 @@ QUnit.module('Views', {
'buttons should have correct icon');
list.$('.o_list_button:first > button').click(); // click on the button
assert.verifySteps(['/web/dataset/search_read', '/web/dataset/search_read'],
"should have reloaded the view (after the action is complete)");
list.destroy();
});