Help

14

How can I extend a JS web module ?

There's some javascript code in a module that I want to modify, is there a way to do that in a module, like in python ?

Avatar
Discard
4 Answers
25
Best Answer

Yes! First You'll need to create your own module, and set the module you want to modify as a dependency of your new module in the __openerp__.py file : 'depends': ['module_I_want_to_extend'],

Then you need to create your javascript file, and put it in the standard place : module/static/src/js/file.js and then add to the list of js files to load in __openerp__.py : js:['static/src/js/file.js'],

file.js will be executed after the code of the module you want to extend, so you have the oportunity to redefine and modify everything you want. Here's a sample js file that overrides the bar() method of the Foo class of the sample module.

openerp.new_module = function(instance){
    var module = instance.sample // loading the namespace of the 'sample' module

    module.Foo.include({
        bar : function(){
            console.log('Hello!');
            this._super();  // calling the original Foo.bar() method
        },
    });
};
5 Comments
Avatar
Discard
Avatar
Nicolas Vanhoren (niv)
-

That'll work, but... if the Foo class has sub classes, those sub classes will not inherit your modifications. If you need to do that, you can use include() instead of extend().

That is true, I'll edit the answer ( I originally used module.Foo.extend({...}) )

Avatar
Fabien Meghazi (fme)
-

You got an error in the code example : module.Foo = module.Foo.include({... will assign undefined to module.Foo. You just have to use module.Foo.include({... alone, this will "edit in place"

owkay ! edited.

Avatar
fussions
-

I have a situation when my module loads I want to make a call to another module which is not jet in instance object. Is there any way to delay initialization of my module till all other modules are in instance object?

10
Best Answer

The technical documentation is located here:

http://doc.openerp.com/trunk/

In the web part, you will find documentation on how to write web modules and some reference documentation.

After that, there are many tricks to override a module behavior. A typical one would be to use the Class.include() method to modify an existing class, overriding whatever methods it defines.

Avatar
Discard
6
Best Answer

Oh, and by the way, the answer above (cannot edit...) Only works if module.Foo extends instance.web.Class native JS objects, or Backbone objects do not provide an include() method. So you have to do it by hand.

openerp.new_module = function(instance){
    var module = instance.sample; // loading the namespace of the 'sample' module

    var _super_ = module.Foo.prototype.bar;

    module.Foo.prototype.bar = function(){
         console.log('Hello!');
         _super_.call(this);
    };
};
1 Comment
Avatar
Discard
Avatar
Shrikant
-

Hello, I am trying to inheriting var Pos from point_of_sale from openerp 6.1 module to override get_app_data() function please help me. Here is my code for referance which gives error "module is undefined": This is my inherited js file:

openerp.my_pos = function(instance){

instance.my_pos = {};

var module = instance.point_of_sale;

var _super_ = module.Pos.prototype.get_app_data;

module.Pos.prototype.get_app_data = function(){
     console.log('Hello!');
     _super_.call(this);
};

};

0
Avatar
Carlos Almeida
Best Answer

How to load js, in the web/database/manager form?

It seems this doesn't work on login form or before login

I even started odoo with --load web,mymodule adding .js in assets and they are not loaded.


Avatar
Discard