openerp Web 客户端 6.1:如何覆盖基本 javascript 函数
我正在寻找一种方法来覆盖一些 openerp web js 核心功能,例如“on_logout”。
该文档缺乏说明(如您在 我的帖子) 和 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 已加载,因为在此定义之外放置警报是有效的。
这是怎么回事?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个有点特殊的问题,因为您想要更改已经实例化的对象的原型(类,如果您愿意的话)(WebClient 实例是系统的根,因此在您的代码编写时它可能已经存在)已加载,因此创建新的 WebClient“类”不会改变现有实例)。
在这种情况下,你不能用子类替换该类,你必须重新打开该类(以类似于 Ruby 的方式),因为类对象上有一个
include
方法,哪个应该工作:(就像在Ruby中一样,
this._super
绑定到您要替换的方法(如果有的话)以进行就地类更改)如果您检查view_list_editable.js 实现文件,它提供了示例因为它需要重新打开并更改列表视图的代码以添加可编辑性。
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:(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.