Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

openerp web client 6.1: how to override base javascript functions

I'm looking for a way to override some openerp web js core functions such as "on_logout".

The docs lack of instructions (as you can see in my post) and the helloworld module tells you that you can do it like

openerp.web_hello = function(openerp) {

openerp.web.SearchView = openerp.web.SearchView.extend({
    init:function() {
        this._super.apply(this,arguments);
        this.on_search.add(function(){console.log('hello');});
    }
});

// here you may tweak globals object, if any, and play with on_* or do_* callbacks on them

openerp.web.Login = openerp.web.Login.extend({
    start: function() {
        console.log('Hello there');
        this._super.apply(this,arguments);
    }
});

};

In my module I'm doing this:

openerp.mytest = function(openerp){

    openerp.web.WebClient = openerp.web.WebClient.extend({
        on_logout: function() {
            alert('mine');
            [...]
        },
    });
}

I know js is loaded since putting an alert outside this definition works.

What's wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's a bit of a special issue since you want to alter the prototype (the class, if you will) of an object which is already instantiated (a WebClient instance is the root of the system, so it's probably already there by the time your code is loaded, therefore creating a new WebClient "class" won't alter the existing instance).

In that case, you can't replace the class with a subclass, you have to re-open the class (in a manner similar to Ruby), for that there is an include method on class objects, which should work:

openerp.mytest = function(openerp) {
    openerp.web.WebClient.include({
        on_logout: function() {
            alert('mine');
            this._super.apply(this, arguments);
        }
    });
}

(as in Ruby, this._super is bound to the method you're replacing, if any, for in-place class alterations)

If you check the view_list_editable.js implementation file, it provides examples of that since it needs to reopen and alter the listview's code in order to add editability.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...