diff options
| author | Pierre Paridans <app@odoo.com> | 2019-02-27 11:53:02 +0000 |
|---|---|---|
| committer | Pierre Paridans <app@odoo.com> | 2019-03-13 09:14:47 +0000 |
| commit | ad8a6afa4fbe89ecbbc027f00af5cc2bac025571 (patch) | |
| tree | f051f90fd3eca4bbdba900a460db5b00c87ff8fc /addons/lunch/static | |
| parent | 0ab333c6e837f08cbe13a2b533ab2c7422647724 (diff) | |
[FIX] lunch: rpc call cannot be performed in Widget.init()
The Widget's init function is synchronous.
The 'then' will execute after the end of the function so
self.group_portal_id will not be set immediately.
Also, RPC's in the init function is really not a good idea, it should be
putted in the willStart.
Original commit: https://github.com/odoo/odoo/commit/855c6dac25fccaf9312543022001ed0b360d6e13
closes odoo/odoo#31508
Signed-off-by: Adrien Dieudonné (adr) <adr@odoo.com>
Diffstat (limited to 'addons/lunch/static')
| -rw-r--r-- | addons/lunch/static/src/js/lunch_kanban_widget.js | 34 | ||||
| -rw-r--r-- | addons/lunch/static/tests/test_lunch_kanban.js | 36 |
2 files changed, 55 insertions, 15 deletions
diff --git a/addons/lunch/static/src/js/lunch_kanban_widget.js b/addons/lunch/static/src/js/lunch_kanban_widget.js index 3cd3230faa1..73721c6def1 100644 --- a/addons/lunch/static/src/js/lunch_kanban_widget.js +++ b/addons/lunch/static/src/js/lunch_kanban_widget.js @@ -33,7 +33,6 @@ var LunchKanbanWidget = Widget.extend({ }, init: function (parent, params) { - var self = this; this._super.apply(this, arguments); this.is_manager = params.is_manager || false; @@ -44,20 +43,6 @@ var LunchKanbanWidget = Widget.extend({ this.group_portal_id = undefined; - self._rpc({ - model: 'ir.model.data', - method: 'xmlid_to_res_id', - kwargs: {xmlid: 'base.group_portal'}, - }).then(function (id) { - self.group_portal_id = id; - }); - - if (this.is_manager) { - this.lunchUserField = this._createMany2One('users', 'res.users', this.username, function () { - return [['groups_id', 'not in', [self.group_portal_id]]]; - }); - } - this.locations = params.locations || []; this.userLocation = params.user_location[1] || ''; @@ -73,6 +58,25 @@ var LunchKanbanWidget = Widget.extend({ this.currency = params.currency || session.get_currency(session.company_currency_id); }, + willStart: function () { + var self = this; + var superDef = this._super.apply(this, arguments); + + var def = this._rpc({ + model: 'ir.model.data', + method: 'xmlid_to_res_id', + kwargs: {xmlid: 'base.group_portal'}, + }).then(function (id) { + self.group_portal_id = id; + + if (self.is_manager) { + self.lunchUserField = self._createMany2One('users', 'res.users', self.username, function () { + return [['groups_id', 'not in', [self.group_portal_id]]]; + }); + } + }); + return $.when(superDef, def); + }, renderElement: function () { this._super.apply(this, arguments); if (this.lunchUserField) { diff --git a/addons/lunch/static/tests/test_lunch_kanban.js b/addons/lunch/static/tests/test_lunch_kanban.js index 38d27fafda7..020c3748150 100644 --- a/addons/lunch/static/tests/test_lunch_kanban.js +++ b/addons/lunch/static/tests/test_lunch_kanban.js @@ -2,6 +2,7 @@ odoo.define('lunch.lunchKanbanTests', function (require) { "use strict"; var LunchKanbanView = require('lunch.LunchKanbanView'); +var LunchKanbanWidget = require('lunch.LunchKanbanWidget'); var testUtils = require('web.test_utils'); var createView = testUtils.createView; @@ -218,6 +219,41 @@ QUnit.module('Views', { lunchKanban.destroy(); }); + + QUnit.module('LunchKanbanWidget'); + + QUnit.test('Rpc calls should be performed after the init()', function (assert) { + assert.expect(2); + + var parent = testUtils.createParent({}); + + var widget = new LunchKanbanWidget(parent, { + user_location: [1, 'hello'], + }); + + testUtils.mock.addMockEnvironment(widget, { + data: {}, + mockRPC: function (route, args) { + if (route === '/web/dataset/call_kw/ir.model.data/xmlid_to_res_id') { + assert.ok('should perform xmlid_to_res_id RPC call in the willStart()'); + assert.deepEqual(args, { + args: [], + kwargs: { + xmlid: "base.group_portal", + }, + method: "xmlid_to_res_id", + model: "ir.model.data", + }, 'should have the right parameters for xmlid_to_res_id RPC call'); + return $.when(); + } + return this._super.apply(this, arguments); + }, + }); + + widget.appendTo($("#qunit-fixture")); + + parent.destroy(); + }); }); }); |
