From e7ad2754a7c28132fefb40fda6ac486ba614c021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 28 Nov 2016 00:14:45 +0100 Subject: [PATCH] Set default value for new added fields to model group Defines add_fields on Group and uses it instead of the one on Model. This new method is used when setting group on Screen to ensure to trigger the load of default values. issue6058 review29781003 --- src/model.js | 27 +++++++++++++++++++++++++-- src/screen.js | 11 +++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/model.js b/src/model.js index 06d6bf6706..b4c0287c16 100644 --- a/src/model.js +++ b/src/model.js @@ -11,14 +11,17 @@ this.fields = {}; }, add_fields: function(descriptions) { + var added = []; for (var name in descriptions) { if (descriptions.hasOwnProperty(name) && (!(name in this.fields))) { var desc = descriptions[name]; var Field = Sao.field.get(desc.type); this.fields[name] = new Field(desc); + added.push(name); } } + return added; }, execute: function(method, params, context) { if (context === undefined) { @@ -339,6 +342,26 @@ this.parent.group.children.push(this); } }; + array.add_fields = function(fields) { + var added = this.model.add_fields(fields); + if (jQuery.isEmptyObject(this)) { + return; + } + var new_ = []; + this.forEach(function(record) { + if (record.id < 0) { + new_.push(record); + } + }); + if (new_.length && added.length) { + this.model.execute('default_get', [added, this.context()]) + .then(function(values) { + new_.forEach(function(record) { + record.set_default(values); + }); + }); + } + }; array.destroy = function() { if (this.parent) { var i = this.parent.group.children.indexOf(this); @@ -1892,7 +1915,7 @@ var set_value = function(fields) { var promises = []; if (!jQuery.isEmptyObject(fields)) { - group.model.add_fields(fields); + group.add_fields(fields); } record._values[this.name] = group; if (mode == 'list ids') { @@ -2098,7 +2121,7 @@ if (value.add || value.update) { prm.then(function(fields) { - group.model.add_fields(fields); + group.add_fields(fields); if (value.add) { value.add.forEach(function(vals) { var index = vals[0]; diff --git a/src/screen.js b/src/screen.js index ce82a1185c..8b423c1798 100644 --- a/src/screen.js +++ b/src/screen.js @@ -729,7 +729,7 @@ .description.loading; } } - this.model.add_fields(fields); + this.group.add_fields(fields); var view_widget = Sao.View.parse(this, xml_view, view.field_childs); view_widget.view_id = view_id; this.views.push(view_widget); @@ -890,8 +890,14 @@ }.bind(this)); }, set_group: function(group) { + var fields = {}; if (this.group) { - jQuery.extend(group.model.fields, this.group.model.fields); + for (var name in this.group.model.fields) { + if (!this.group.model.fields.hasOwnProperty(name)) { + continue; + } + fields[name] = this.group.model.fields[name].description; + } this.group.screens.splice( this.group.screens.indexOf(this), 1); jQuery.extend(group.on_write, this.group.on_write); @@ -911,6 +917,7 @@ } else { this.set_current_record(group[0]); } + this.group.add_fields(fields); }, new_group: function(ids) { var group = new Sao.Group(this.model, this.context, []);