diff --git a/addons/web/static/src/js/view_list_editable.js b/addons/web/static/src/js/view_list_editable.js index 37ce8c4f8b4..f144b5485c7 100644 --- a/addons/web/static/src/js/view_list_editable.js +++ b/addons/web/static/src/js/view_list_editable.js @@ -172,6 +172,22 @@ openerp.web.list_editable = function (instance) { return self.save_edition(); }); }, + /** + * Builds a record with the provided id (``false`` for a creation), + * setting all columns with ``false`` value so code which relies on + * having an actual value behaves correctly + * + * @param {*} id + * @return {instance.web.list.Record} + */ + make_empty_record: function (id) { + var attrs = {id: id}; + _(this.columns).chain() + .filter(function (x) { return x.tag === 'field'}) + .pluck('name') + .each(function (field) { attrs[field] = false; }); + return new instance.web.list.Record(attrs); + }, /** * Set up the edition of a record of the list view "inline" * @@ -186,12 +202,7 @@ openerp.web.list_editable = function (instance) { if (record) { item = record.attributes; } else { - var attrs = {id: false}; - _(this.columns).chain() - .filter(function (x) { return x.tag === 'field'}) - .pluck('name') - .each(function (field) { attrs[field] = false; }); - record = new instance.web.list.Record(attrs); + record = this.make_empty_record(false); this.records.add(record, { at: this.prepends_on_create() ? 0 : null}); } @@ -391,7 +402,7 @@ openerp.web.list_editable = function (instance) { if (!record) { // insert after the source record var index = this.records.indexOf(source_record) + 1; - record = new instance.web.list.Record({id: id}); + record = this.make_empty_record(id); this.records.add(record, {at: index}); this.dataset.ids.splice(index, 0, id); } diff --git a/addons/web/static/test/list-editable.js b/addons/web/static/test/list-editable.js index 459a6df397a..282ef70f97b 100644 --- a/addons/web/static/test/list-editable.js +++ b/addons/web/static/test/list-editable.js @@ -346,4 +346,80 @@ $(document).ready(function () { }) .fail(function (e) { ok(false, e && e.message || e); }); }); + + module('list-edition-onwrite', { + setup: function () { + baseSetup(); + } + }); + + asyncTest('record-to-read', 4, function () { + instance.session.responses['/web/view/load'] = function () { + return {result: { + type: 'tree', + fields: { + a: {type: 'char', string: "A"} + }, + arch: { + tag: 'tree', + attrs: { on_write: 'on_write', colors: 'red:a == "foo"' }, + children: [ + {tag: 'field', attrs: {name: 'a'}} + ] + } + }}; + }; + instance.session.responses['/web/dataset/call_kw:read'] = function (req) { + if (_.isEmpty(req.params.args[0])) { + return {result: []}; + } else if (_.isEqual(req.params.args[0], [1])) { + return {result: [ + {id: 1, a: 'some value'} + ]}; + } else if (_.isEqual(req.params.args[0], [42])) { + return {result: [ + {id: 42, a: 'foo'} + ]}; + } + throw new Error(JSON.stringify(req.params)); + }; + instance.session.responses['/web/dataset/call_kw:default_get'] = function () { + return {result: {}}; + }; + instance.session.responses['/web/dataset/call_kw:create'] = function () { + return {result: 1}; + }; + instance.session.responses['/web/dataset/call_kw:on_write'] = function () { + return {result: [42]}; + }; + + var ds = new instance.web.DataSetStatic(null, 'demo', null, []); + var l = new instance.web.ListView({}, ds, false, {editable: 'top'}); + l.appendTo($fix) + .then(l.proxy('reload_content')) + .then(function () { + return l.start_edition(); + }) + .then(function () { + $fix.find('.oe_form_field input').val("some value").change(); + }) + .then(function () { + return l.save_edition(); + }) + .always(function () { start(); }) + .then(function () { + strictEqual(ds.ids.length, 2, + 'should have id of created + on_write'); + strictEqual(l.records.length, 2, + 'should have record of created + on_write'); + strictEqual( + $fix.find('tbody tr:eq(1)').css('color'), 'rgb(255, 0, 0)', + 'shoud have color applied'); + strictEqual( + $fix.find('tbody tr:eq(2)').css('color'), 'rgb(0, 0, 0)', + 'should have default color applied'); + }, function (e) { + ok(false, e && e.message || e); + }); + }); });