Examples below demonstrate how to call Python function from JavaScript in Odoo 8. In the examples we call methods of my_model described early on this page.
We assume that in the following examples "list_of_ids" variable contains list(array) of ids of existing records of "my.model" model.
new instance.web.Model("my.model") .call( "foo_manipulate_records_1", [list_of_ids]) .then(function (result) { // do something with result });
new instance.web.Model("my.model") .call( "foo_manipulate_records_2", [list_of_ids, arg1, arg2]) .then(function (result) { // do something with result });
new instance.web.Model("my.model") .call( "bar_no_deal_with_ids", [arg1, arg2]) .then(function (result) { // do something with result });
Also if it has some sense depending on implementation, then you can call function decorated with @api.multi even if you have not to deal with ids (just pass empty array in place of ids, as first element of argument list):
new instance.web.Model("my.model") .call( "foo_manipulate_records_2", [[], arg1, arg2]) .then(function (result) { // do something with result });
this way may be useful in some cases, as undecorated function in v8.0 api is considered as @api.multi (as @api.multi is a default decorator)
Cxcept of two parameters to RPC call that are used in the above examples (the function name and argument list), you can use third parameter - a dictionary of keyword arguments. It's highly recommended to turn around a context (in some cases it might be even necessary), as it may change behavior of remote procedure (localization, etc.). See below the example with context argument in RPC call (same may be applied to all examples above)
var self = this;
new instance.web.Model("my.model")
.call("foo_manipulate_records_2", [[], arg1, arg2], {'context':self.session.user_context})
.then(function (result) {
// do something with result
});
Of course you can use custom context as well, if necessary, instead of turning around the existing one as in this example.