微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

openerp web client 6.1:如何覆盖基本的javascript函数

我正在寻找一种方法来覆盖一些openerp web js核心功能,如“on_logout”.

文档缺乏说明(如您在my post中所见),helloworld module告诉您可以这样做

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

};

在我的模块中,我这样做:

openerp.mytest = function(openerp){

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

我知道js已加载,因为在此定义之外放置警报有效.

这有什么不对?

解决方法

这是一个特殊的问题,因为您想要更改已经实例化的对象的原型(类,如果您愿意)(WebClient实例是系统的根,所以它可能已经存在于您的代码时加载,因此创建一个新的WebClient“类”将不会改变现有的实例).

在这种情况下,你不能用子类替换类,你必须重新打开类(以类似于Ruby的方式),因为在类对象上有一个include方法,它应该工作:

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

(如在Ruby中,this._super绑定到您要替换的方法,如果有的话,用于就地类更改)

如果检查view_list_editable.js实现文件,它会提供示例,因为它需要重新打开并更改listview的代码才能添加可编辑性.

原文地址:https://www.jb51.cc/html/231603.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐