diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 4ae30264ff0..1946cfeb529 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -1337,7 +1337,6 @@ var FieldSelection = AbstractField.extend({ */ init: function () { this._super.apply(this, arguments); - this.values = []; if (this.field.type === 'many2one') { this.values = this.record.specialData[this.name]; } else { @@ -1352,35 +1351,24 @@ var FieldSelection = AbstractField.extend({ // Private //-------------------------------------------------------------------------- - /** - * Format the value to a valid string - * - * @override - * @private - * @param {any} value - * @returns {string} - */ - _formatValue: function (value) { - if (this.field.type === 'many2one') { - var options = _.extend({}, this.nodeOptions); - return field_utils.format.many2one(value, this.field, this.recordData, options); - } else { - return this._super(value); - } - }, /** * @override * @private */ _renderEdit: function () { - this.$el.empty(); - for (var i = 0 ; i < this.values.length ; i++) { - this.$el.append($('', { - value: JSON.stringify(this.values[i][0]), - html: this.values[i][1] - })); + if (!this.$el.children().length) { + for (var i = 0 ; i < this.values.length ; i++) { + this.$el.append($('', { + value: JSON.stringify(this.values[i][0]), + html: this.values[i][1] + })); + } } - this.$el.val(JSON.stringify(this._parseValue(this.value))); + var value = this.value; + if (this.field.type === 'many2one' && value) { + value = value.data.id; + } + this.$el.val(JSON.stringify(value)); }, /** * @override @@ -1389,16 +1377,6 @@ var FieldSelection = AbstractField.extend({ _renderReadonly: function () { this.$el.empty().html(this._formatValue(this.value)); }, - /** - * @override - * @private - */ - _reset: function () { - this._super.apply(this, arguments); - if (this.field.type === 'many2one') { - this.value = this.value.data.id; - } - }, //-------------------------------------------------------------------------- // Handlers @@ -1407,6 +1385,7 @@ var FieldSelection = AbstractField.extend({ /** * The small slight difficulty is that we have to set the value differently * depending on the field type. + * * @private */ _onChange: function () { diff --git a/addons/web/static/test/fields/relational_fields_tests.js b/addons/web/static/test/fields/relational_fields_tests.js index 7b497ca943b..518e59b338f 100644 --- a/addons/web/static/test/fields/relational_fields_tests.js +++ b/addons/web/static/test/fields/relational_fields_tests.js @@ -3196,8 +3196,12 @@ QUnit.module('relational_fields', { list.destroy(); }); - QUnit.test('widget selection on a many2one field', function (assert) { - assert.expect(7); + QUnit.test('widget selection, edition and on many2one field', function (assert) { + assert.expect(15); + + this.data.partner.onchanges = {product_id: function () {}}; + this.data.partner.records[0].product_id = 37; + this.data.partner.records[0].trululu = false; var count = 0; var form = createView({ @@ -3206,10 +3210,13 @@ QUnit.module('relational_fields', { data: this.data, arch: '
', res_id: 1, mockRPC: function (route, args) { count++; + assert.step(args.method); return this._super(route, args); }, }); @@ -3217,14 +3224,25 @@ QUnit.module('relational_fields', { assert.ok(!form.$('select').length, "should not have a select tag in dom"); form.$buttons.find('.o_form_button_edit').click(); - assert.ok(form.$('select').length, "should have a select tag in dom"); - assert.ok(form.$('option:contains(xphone)').length, "should have fetched xphone option"); - assert.ok(form.$('option:contains(xpad)').length, "should have fetched xpad option"); + assert.strictEqual(form.$('select').length, 3, + "should have 3 select tag in dom"); + assert.strictEqual(form.$('select[name="product_id"] option:contains(xphone)').length, 1, + "should have fetched xphone option"); + assert.strictEqual(form.$('select[name="product_id"] option:contains(xpad)').length, 1, + "should have fetched xpad option"); + assert.strictEqual(form.$('select[name="product_id"]').val(), "37", + "should have correct product_id value"); + assert.strictEqual(form.$('select[name="trululu"]').val(), "false", + "should not have any value in trululu field"); + form.$('select[name="product_id"]').val(41).trigger('change'); - assert.strictEqual(form.$('select').val(), "false", "should not have any value"); - form.$('select').val(37).trigger('input'); - assert.strictEqual(form.$('select').val(), "37", "should have a value of xphone"); + assert.strictEqual(form.$('select[name="product_id"]').val(), "41", + "should have a value of xphone"); + assert.strictEqual(form.$('select[name="color"]').val(), "\"red\"", + "should have correct value in color field"); + + assert.verifySteps(['read', 'name_search', 'name_search', 'onchange']); count = 0; form.reload(); assert.strictEqual(count, 1, "should not reload product_id relation");