openerp Web 客户端 6.1:如何覆盖基本的 javascript 函数 [英] openerp web client 6.1: how to override base javascript functions

查看:39
本文介绍了openerp Web 客户端 6.1:如何覆盖基本的 javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

文档缺乏说明(如您在 我的帖子) 和 helloworld 模块 告诉你你可以这样做

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');
            [...]
        },
    });
}

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

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

这里出了什么问题?

推荐答案

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

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

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

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

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

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

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

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.

这篇关于openerp Web 客户端 6.1:如何覆盖基本的 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆