In Odoo 16, when working with JavaScript code, there are two methods available for extending or modifying existing functionality: extend and include. Both methods are used in Odoo’s class system to inherit and modify behavior, but they have different purposes and usage scenarios.
Extend Method (extend): The extend method is used to create a new class that inherits from an existing class and adds or overrides its methods. It allows you to extend the functionality of an existing class without modifying the original class directly.
Example 1: Extending an existing class.
odoo.define('my_module.MyClassExtension', function (require) {
'use strict';
var core = require('web.core');
var MyClass = require('some.module.MyClass');
var _t = core._t;
var MyExtendedClass = MyClass.extend({
// New methods or overridden methods
myNewMethod: function () {
// New functionality added to MyExtendedClass
},
});
return MyExtendedClass;
});
In this example, we define a new module, my_module.MyClassExtension that extends some.module.MyClass.
We create a new class, MyExtendedClass by using MyClass.extend() and add a new method,myNewMethod.
This method can contain new functionality or override existing methods.
Example 2: Accessing and modifying parent class methods.
odoo.define('my_module.MyClassExtension', function (require) {
'use strict';
var core = require('web.core');
var MyClass = require('some.module.MyClass');
var _t = core._t;
MyClass.extend({
myMethod: function () {
// Calling parent class method
this._super();
// Adding new functionality to the parent method
// ...
},
});
return MyClass;
});
In this example, we use the include method to modify an existing method of the parent class MyClass.
By calling this._super(), we can execute the parent class method and then add new functionality or modify the behavior of that method.
Include Method (include): The include method is used to add additional behavior or properties to an existing class without overriding any existing methods. It is primarily used for mixins or to add generic behavior to multiple classes.
Example 1: Using a mixin to add common functionality.
odoo.define('my_module.MyMixin', function (require) {
'use strict';
var core = require('web.core');
var _t = core._t;
var MyMixin = {
myMixinMethod: function () {
// Mixin method implementation
},
};
return MyMixin;
});
odoo.define('my_module.MyClassExtension', function (require) {
'use strict';
var core = require('web.core');
var MyClass = require('some.module.MyClass');
var MyMixin = require('my_module.MyMixin');
var _t = core._t;
MyClass.include(MyMixin);
return MyClass;
});
In this example, we define a mixin my_module.MyMixin provides the myMixinMethod.
Then, we use the include method to include this mixin into the MyClass from the some.module.MyClass.
The myMixinMethod is now available in MyClass and can be used by instances of MyClass.
Example 2: Adding a generic behavior to multiple classes.
odoo.define('my_module.GenericBehavior', function (require) {
'use strict';
var core = require('web.core');
var _t = core._t;
var GenericBehavior = {
genericMethod: function () {
// Generic behaviour implementation
},
};