From 414eb53d01feae9e1358f36b9534a3a3b6ed108a Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Thu, 20 Apr 2017 16:16:10 +0200 Subject: [PATCH] [FIX] web: can open a record with a virtual id Before this change, the switch view open a create form instead of open the record. eg: click to open a recurrent event in calendar list view --- .../static/src/js/views/basic/basic_model.js | 24 +++++++++---- addons/web/static/tests/views/list_tests.js | 36 ++++++++++++++++++- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js index 4b39025b35d..87c9f3c57bc 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -51,9 +51,10 @@ odoo.define('web.BasicModel', function (require) { * * Notes: * - id: is totally unrelated to res_id. id is a web client local concept - * - res_id: if set to a number, it is an actual id for a record in the server - * database. If set to 'virtual_' + number, it is a record not yet saved (so, - * in create mode). + * - res_id: if set to a number or a virtual id (a virtual id is a character + * string composed of an integer and has a dash and other information), it + * is an actual id for a record in the server database. If set to + * 'virtual_' + number, it is a record not yet saved (so, in create mode). * - res_ids: if set, it represent the context in which the data point is actually * used. For example, a given record in a form view (opened from a list view) * might have a res_id = 2 and res_ids = [1,2,3] @@ -397,14 +398,25 @@ var BasicModel = AbstractModel.extend({ return isDirty; }, /** - * Check if a record is new, meaning if it is in the process of being - * created and no actual record exists in db. + * Check if a record is new, meaning if it is in the process of being created + * and no actual record exists in db. + * + * Note: A virtual id is a character string composed of an integer and has + * a dash and other information. + * E.g: in calendar, the recursive event have virtual id linked to a real id + * virtual event id "23-20170418020000" is linked to the event id 23 * * @param {string} id id for a local resource * @returns {boolean} */ isNew: function (id) { - return typeof this.localData[id].res_id !== 'number'; + var res_id = this.localData[id].res_id; + if (typeof res_id === 'number') { + return false; + } else if (typeof res_id === 'string' && /^[0-9]+-/.test(res_id)) { + return false; + } + return true; }, /** * Main entry point, the goal of this method is to fetch and process all diff --git a/addons/web/static/tests/views/list_tests.js b/addons/web/static/tests/views/list_tests.js index 3b14f15a775..59a7aa774b8 100644 --- a/addons/web/static/tests/views/list_tests.js +++ b/addons/web/static/tests/views/list_tests.js @@ -67,7 +67,16 @@ QUnit.module('Views', { {id: 1, display_name: "USD", symbol: '$', position: 'before'}, {id: 2, display_name: "EUR", symbol: '€', position: 'after'}, ], - } + }, + event: { + fields: { + id: {string: "ID", type: "integer"}, + name: {string: "name", type: "char"}, + }, + records: [ + {id: "2-20170808020000", name: "virtual"}, + ] + }, }; } }, function () { @@ -1487,6 +1496,31 @@ QUnit.module('Views', { list.destroy(); }); + + QUnit.test('open a virtual id', function (assert) { + assert.expect(1); + + var list = createView({ + View: ListView, + model: 'event', + data: this.data, + arch: '', + }); + + testUtils.intercept(list, "switch_view", function (event) { + assert.deepEqual(event.data, { + 'model': 'event', + 'res_id': '2-20170808020000', + 'view_type': 'form', + 'mode': 'readonly' + }, + "should trigger switch to the form view for the record virtual id"); + }); + list.$('td:contains(virtual)').click(); + + list.destroy(); + }); + }); });