diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js index b63d44a408d..f333f6a3e4b 100644 --- a/addons/web/static/src/js/data.js +++ b/addons/web/static/src/js/data.js @@ -253,19 +253,11 @@ instance.web.QueryGroup = instance.web.Class.extend({ } }); -instance.web.Model = instance.web.Class.extend({ - /** - * @constructs instance.web.Model - * @extends instance.web.Class - * - * @param {String} model_name name of the OpenERP model this object is bound to - * @param {Object} [context] - * @param {Array} [domain] - */ - init: function (model_name, context, domain) { - this.name = model_name; - this._context = context || {}; - this._domain = domain || []; +instance.web.Model.include({ + session: function() { + if (! this._session) + return instance.session; + return this._super(); }, /** * @deprecated does not allow to specify kwargs, directly use call() instead @@ -294,13 +286,7 @@ instance.web.Model = instance.web.Class.extend({ args = []; } instance.web.pyeval.ensure_evaluated(args, kwargs); - var debug = instance.session.debug ? '/'+this.name+':'+method : ''; - return instance.session.rpc('/web/dataset/call_kw' + debug, { - model: this.name, - method: method, - args: args, - kwargs: kwargs - }, options); + return this._super(method, args, kwargs, options); }, /** * Fetches a Query instance bound to this model, for searching diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index c80ee9ffcdb..d3dd5f786a0 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -1050,6 +1050,69 @@ openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { } }); +openerp.web.Model = openerp.web.Class.extend({ + /** + new openerp.web.Model([session,] model_name[, context[, domain]]) + + @constructs instance.web.Model + @extends instance.web.Class + + @param {openerp.web.Session} [session] The session object used to communicate with + the server. + @param {String} model_name name of the OpenERP model this object is bound to + @param {Object} [context] + @param {Array} [domain] + */ + init: function () { + var session, model_name, context, domain; + var args = _.toArray(arguments); + args.reverse(); + session = args.pop(); + if (! (session instanceof openerp.web.Session)) { + model_name = session; + session = null; + } else { + model_name = args.pop(); + } + context = args.length > 0 ? args.pop() : null; + domain = args.length > 0 ? args.pop() : null; + + this.name = model_name; + this._context = context || {}; + this._domain = domain || []; + this._session = session; + }, + session: function() { + if (! this._session) + throw new Error("Not implemented"); + return this._session; + }, + /** + * Call a method (over RPC) on the bound OpenERP model. + * + * @param {String} method name of the method to call + * @param {Array} [args] positional arguments + * @param {Object} [kwargs] keyword arguments + * @param {Object} [options] additional options for the rpc() method + * @returns {jQuery.Deferred<>} call result + */ + call: function (method, args, kwargs, options) { + args = args || []; + kwargs = kwargs || {}; + if (!_.isArray(args)) { + // call(method, kwargs) + kwargs = args; + args = []; + } + return this.session().rpc('/web/dataset/call_kw', { + model: this.name, + method: method, + args: args, + kwargs: kwargs + }, options); + }, +}); + openerp.web.get_cookie = function(c_name) { if (document.cookie.length > 0) { var c_start = document.cookie.indexOf(c_name + "=");