[FIX] onchange for client-side evaluation: use regular method calls
bzr revid: xmo@openerp.com-20121127133435-ss3g97sm43mrtbla
This commit is contained in:
parent
f243fd7c46
commit
908c986ce8
|
@ -1069,28 +1069,6 @@ class DataSet(openerpweb.Controller):
|
||||||
|
|
||||||
return getattr(req.session.model(model), method)(*args, **kwargs)
|
return getattr(req.session.model(model), method)(*args, **kwargs)
|
||||||
|
|
||||||
@openerpweb.jsonrequest
|
|
||||||
def onchange(self, req, model, method, args, context_id=None):
|
|
||||||
""" Support method for handling onchange calls: behaves much like call
|
|
||||||
with the following differences:
|
|
||||||
|
|
||||||
* Does not take a domain_id
|
|
||||||
* Is aware of the return value's structure, and will parse the domains
|
|
||||||
if needed in order to return either parsed literal domains (in JSON)
|
|
||||||
or non-literal domain instances, allowing those domains to be used
|
|
||||||
from JS
|
|
||||||
|
|
||||||
:param req:
|
|
||||||
:type req: web.common.http.JsonRequest
|
|
||||||
:param str model: object type on which to call the method
|
|
||||||
:param str method: name of the onchange handler method
|
|
||||||
:param list args: arguments to call the onchange handler with
|
|
||||||
:param int context_id: index of the context object in the list of
|
|
||||||
arguments
|
|
||||||
:return: result of the onchange call with all domains parsed
|
|
||||||
"""
|
|
||||||
return self._call_kw(req, model, method, args, {})
|
|
||||||
|
|
||||||
@openerpweb.jsonrequest
|
@openerpweb.jsonrequest
|
||||||
def call(self, req, model, method, args, domain_id=None, context_id=None):
|
def call(self, req, model, method, args, domain_id=None, context_id=None):
|
||||||
return self._call_kw(req, model, method, args, {})
|
return self._call_kw(req, model, method, args, {})
|
||||||
|
|
|
@ -437,26 +437,26 @@ instance.web.FormView = instance.web.View.extend(instance.web.form.FieldManagerM
|
||||||
|
|
||||||
var method = call[1];
|
var method = call[1];
|
||||||
if (!_.str.trim(call[2])) {
|
if (!_.str.trim(call[2])) {
|
||||||
return {method: method, args: [], context_index: null}
|
return {method: method, args: []}
|
||||||
}
|
}
|
||||||
|
|
||||||
var argument_replacement = {
|
var argument_replacement = {
|
||||||
'False': function () {return false;},
|
'False': function () {return false;},
|
||||||
'True': function () {return true;},
|
'True': function () {return true;},
|
||||||
'None': function () {return null;},
|
'None': function () {return null;},
|
||||||
'context': function (i) {
|
'context': function () {
|
||||||
context_index = i;
|
return new instance.web.CompoundContext(
|
||||||
var ctx = new instance.web.CompoundContext(self.dataset.get_context(), widget.build_context() ? widget.build_context() : {});
|
self.dataset.get_context(),
|
||||||
return ctx;
|
widget.build_context() ? widget.build_context() : {});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var parent_fields = null, context_index = null;
|
var parent_fields = null;
|
||||||
var args = _.map(call[2].split(','), function (a, i) {
|
var args = _.map(call[2].split(','), function (a, i) {
|
||||||
var field = _.str.trim(a);
|
var field = _.str.trim(a);
|
||||||
|
|
||||||
// literal constant or context
|
// literal constant or context
|
||||||
if (field in argument_replacement) {
|
if (field in argument_replacement) {
|
||||||
return argument_replacement[field](i);
|
return argument_replacement[field]();
|
||||||
}
|
}
|
||||||
// literal number
|
// literal number
|
||||||
if (/^-?\d+(\.\d+)?$/.test(field)) {
|
if (/^-?\d+(\.\d+)?$/.test(field)) {
|
||||||
|
@ -491,8 +491,7 @@ instance.web.FormView = instance.web.View.extend(instance.web.form.FieldManagerM
|
||||||
|
|
||||||
return {
|
return {
|
||||||
method: method,
|
method: method,
|
||||||
args: args,
|
args: args
|
||||||
context_index: context_index
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
do_onchange: function(widget, processed) {
|
do_onchange: function(widget, processed) {
|
||||||
|
@ -511,18 +510,15 @@ instance.web.FormView = instance.web.View.extend(instance.web.form.FieldManagerM
|
||||||
var on_change = widget.node.attrs.on_change;
|
var on_change = widget.node.attrs.on_change;
|
||||||
if (on_change) {
|
if (on_change) {
|
||||||
var change_spec = self.parse_on_change(on_change, widget);
|
var change_spec = self.parse_on_change(on_change, widget);
|
||||||
def = self.rpc('/web/dataset/onchange', {
|
var id = [self.datarecord.id == null ? [] : [self.datarecord.id]];
|
||||||
model: self.dataset.model,
|
def = new instance.web.Model(self.dataset.model).call(
|
||||||
method: change_spec.method,
|
change_spec.method, id.concat(change_spec.args));
|
||||||
args: [(self.datarecord.id == null ? [] : [self.datarecord.id])].concat(change_spec.args),
|
|
||||||
context_id: change_spec.context_index == undefined ? null : change_spec.context_index + 1
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
def = $.when({});
|
def = $.when({});
|
||||||
}
|
}
|
||||||
return def.then(function(response) {
|
return def.then(function(response) {
|
||||||
if (widget.field['change_default']) {
|
if (widget.field['change_default']) {
|
||||||
var fieldname = widget.name
|
var fieldname = widget.name;
|
||||||
var value_;
|
var value_;
|
||||||
if (response.value && (fieldname in response.value)) {
|
if (response.value && (fieldname in response.value)) {
|
||||||
// Use value from onchange if onchange executed
|
// Use value from onchange if onchange executed
|
||||||
|
@ -534,11 +530,9 @@ instance.web.FormView = instance.web.View.extend(instance.web.form.FieldManagerM
|
||||||
var condition = fieldname + '=' + value_;
|
var condition = fieldname + '=' + value_;
|
||||||
|
|
||||||
if (value_) {
|
if (value_) {
|
||||||
return self.rpc('/web/dataset/call', {
|
return new instance.web.Model('ir.values').call(
|
||||||
model: 'ir.values',
|
'get_defaults', [self.model, condition]
|
||||||
method: 'get_defaults',
|
).then(function (results) {
|
||||||
args: [self.model, condition]
|
|
||||||
}).then(function (results) {
|
|
||||||
if (!results.length) {
|
if (!results.length) {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue