Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

13

How can I extend a JS web module ?

By
Frédéric Van Der Essen (fva)
on 1/31/13, 10:00 AM 12,595 views

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 ?

23

Frédéric Van Der Essen (fva)

--Frédéric Van Der Essen (fva)--
903
| 6 7 5
--Frédéric Van Der Essen (fva)--

Frédéric worked on different core projects at Odoo since 2012. He is a core developer of the Odoo R&D department. He worked on several projects including the design of the current website and the UI of the open chatter communication tool.

Frédéric is also the owner of the PosBox project and the latest versions of the point of sale frontend.

Frédéric Van Der Essen (fva)
On 1/31/13, 10:17 AM

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
        },
    });
};

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().

Nicolas Vanhoren (niv)
on 1/31/13, 10:20 AM

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

Frédéric Van Der Essen (fva)
on 1/31/13, 10:55 AM

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"

Fabien Meghazi (fme)
on 1/31/13, 11:09 AM

owkay ! edited.

Frédéric Van Der Essen (fva)
on 1/31/13, 11:51 AM

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?

fussions
on 3/20/14, 9:58 AM
10

Nicolas Vanhoren (niv)

--Nicolas Vanhoren (niv)--
1144
| 0 0 0
--Nicolas Vanhoren (niv)--
Nicolas Vanhoren (niv)
On 1/31/13, 10:09 AM

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.

4

Frédéric Van Der Essen (fva)

--Frédéric Van Der Essen (fva)--
903
| 6 7 5
--Frédéric Van Der Essen (fva)--

Frédéric worked on different core projects at Odoo since 2012. He is a core developer of the Odoo R&D department. He worked on several projects including the design of the current website and the UI of the open chatter communication tool.

Frédéric is also the owner of the PosBox project and the latest versions of the point of sale frontend.

Frédéric Van Der Essen (fva)
On 2/19/13, 12:20 PM

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);
    };
};

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);
};

};

Shrikant
on 3/7/13, 5:51 AM
0
Carlos Almeida
On 11/3/15, 2:25 AM

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.


Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 1/31/13, 10:00 AM
Seen: 12595 times
Last updated: 11/3/15, 2:25 AM