[FIX] web: get conditional user defaults

This is possible to set field conditional defaults, if the field has the attribute "change_default".
The defaults are set by the web client, by calling the method "get_defaults" of ir.values model, when the onchange is triggered on the field on which the condition is.

In 7.0, all onchanges were triggered clientside, one by one. On creation, the defaults of default_get method were pushed in the form, and then, as the values of the fields were changed (from null to the default value), all onchanges on which there was default value were triggered.

In 8.0, onchanges are performed server side, all at once. On creation, the onchange method is triggered by default (wether or not there is a default value for them), for all fields (widget param of method do_onchange of view_forms js is undefined, meaning the onchange is not triggered on a specific field, but on all fields). In such a case, we must call the get_defaults method of ir.values model for all fields (having change_default attribute), in order the conditional defaults to be set in the form view.
This commit is contained in:
Denis Ledoux 2014-12-05 17:40:42 +01:00
parent 0bf69d6f82
commit 9996668bad
1 changed files with 40 additions and 31 deletions

View File

@ -511,38 +511,47 @@ instance.web.FormView = instance.web.View.extend(instance.web.form.FieldManagerM
}
this.onchanges_mutex.exec(function(){
return def.then(function(response) {
if (widget && widget.field['change_default']) {
var fieldname = widget.name;
var value_;
if (response.value && (fieldname in response.value)) {
// Use value from onchange if onchange executed
value_ = response.value[fieldname];
} else {
// otherwise get form value for field
value_ = self.fields[fieldname].get_value();
}
var condition = fieldname + '=' + value_;
if (value_) {
return self.alive(new instance.web.Model('ir.values').call(
'get_defaults', [self.model, condition]
)).then(function (results) {
if (!results.length) {
return response;
}
if (!response.value) {
response.value = {};
}
for(var i=0; i<results.length; ++i) {
// [whatever, key, value]
var triplet = results[i];
response.value[triplet[1]] = triplet[2];
}
return response;
});
}
var fields = {};
if (widget){
fields[widget.name] = widget.field;
}
return response;
else{
fields = self.fields_view.fields;
}
var defs = [];
_.each(fields, function(field, fieldname){
if (field && field.change_default) {
var value_;
if (response.value && (fieldname in response.value)) {
// Use value from onchange if onchange executed
value_ = response.value[fieldname];
} else {
// otherwise get form value for field
value_ = self.fields[fieldname].get_value();
}
var condition = fieldname + '=' + value_;
if (value_) {
defs.push(self.alive(new instance.web.Model('ir.values').call(
'get_defaults', [self.model, condition]
)).then(function (results) {
if (!results.length) {
return response;
}
if (!response.value) {
response.value = {};
}
for(var i=0; i<results.length; ++i) {
// [whatever, key, value]
var triplet = results[i];
response.value[triplet[1]] = triplet[2];
}
return response;
}));
}
}
});
return _.isEmpty(defs) ? response : $.when.apply(null, defs);
}).then(function(response) {
return self.on_processed_onchange(response);
});