RPC calls
In Odoo, If we want to trigger a python function, it can be done with the help of _rpc calls.
this._rpc({
model: this.model,
method: 'quick_publish_products',
args: [this.res_id],
}).then(function (result) {
self.do_action(result);
});
In the example above, we made an RPC call and called the quick_publish_products method defined in the current model.
After calling a method, ._rpc returns the method's data.
New view creation
In Odoo, there are different types of views, and each view provides different purposes and functionality.
Let’s look at how we can create a new view type in Odoo 16.
Here, we are going to create a new view type called the “hello world” view.
Let's follow the below steps to create a “hello world” view;
1. Add Hello World view in ir.ui.view model
class View(models.Model):
_inherit = "ir.ui.view"
type = fields.Selection(selection_add=[('hello_world', "Hello World")])
2. Add Hello World in view_mode selection by inheriting ir.actions.act_window.view model.
class ActWindowView(models.Model):
_inherit = 'ir.actions.act_window.view'
view_mode = fields.Selection(selection_add=[('hello_world', "Hello World")], ondelete={'hello_world': 'cascade'})
3. When adding a new view type, we need to define a js module and need to import the required variables as shown below;
odoo.define('custom_view_app.HelloWorldView', function (require) {
"use strict";
var AbstractController = require("web.AbstractController");
var AbstractModel = require("web.AbstractModel");
var AbstractRenderer = require("web.AbstractRenderer");
var AbstractView = require("web.AbstractView");
var ViewRegistry = require("web.view_registry");
var HelloWorldController = AbstractController.extend({});
var HelloWorldRenderer = AbstractRenderer.extend({});
var HelloWorldModel = AbstractModel.extend({});
4. Then we follow the Model-view-controller pattern.
var HelloWorldView = AbstractView.extend({
config: _.extend({}, AbstractView.prototype.config, {
Model: HelloWorldModel,
Controller: HelloWorldController,
Renderer: HelloWorldRenderer,
}),
viewType: "hello_world",
}
5. Now register the view like below.
ViewRegistry.add("hello_world", HelloWorldView);
return HelloWorldView;
6. Then on the Renderer function, we can add things that we need to view.
For example, we can add a heading “Hello World” like below.
var HelloWorldRenderer = AbstractRenderer.extend({
className: "o_hello_world_view",
this.$el.append(
$('<h1>').text('Hello World!'),
$('<div id="mapid"/>')
);
return $.when();
},
});