超级骨干
当我重写 Backbone.Model
的 clone()
方法时,有没有办法从我的植入中调用此重写方法?像这样的东西:
var MyModel = Backbone.Model.extend({
clone: function(){
super.clone();//calling the original clone method
}
})
When I override the clone()
method of a Backbone.Model
, is there a way to call this overriden method from my implantation? Something like this:
var MyModel = Backbone.Model.extend({
clone: function(){
super.clone();//calling the original clone method
}
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您将需要使用:
这将使用
this
的上下文调用来自Backbone.Model
的原始clone()
方法(当前模型)。来自 Backbone 文档:
You'll want to use:
This will call the original
clone()
method fromBackbone.Model
with the context ofthis
(The current model).From Backbone docs:
您还可以使用
__super__
属性,它是引用父类原型:You can also use the
__super__
property which is a reference to the parent class prototype:Josh Nielsen 为此找到了一个优雅的解决方案,它隐藏了很多丑陋的地方。
只需将此代码片段添加到您的应用程序中即可扩展 Backbone 的模型:
然后像这样使用它:
Josh Nielsen found an elegant solution for this, which hides a lot of the ugliness.
Just add this snippet to your app to extend Backbone's model:
Then use it like this:
根据 geek_dave 和 charthesisto 给出的答案,我编写了此代码以在具有多个继承级别的类中添加
this._super(funcName, ...)
支持。它在我的代码中运行良好。一年后,我修复了一些错误并使事情变得更快。下面是我现在使用的代码。
然后给出这段代码:
控制台中的输出是这样的:
有趣的是,C 类对 this._super("go1") 的调用会扫描类层次结构,直到它在 A 类中命中。解决方案不会这样做。
PS 取消注释类定义的
className
条目以启用_super
查找的缓存。 (假设这些类名在应用程序中是唯一的。)Working from the answers given by geek_dave and charlysisto, I wrote this to add
this._super(funcName, ...)
support in classes that have multiple levels of inheritance. It's worked well in my code.A year later I've fixed some bugs and made things faster. Below is the code that I use now.
Then given this code:
The output in the console is this:
What's interesting is that class C's call to
this._super("go1")
scans the class hierarchy until it gets a hit in class A. Other solutions do not do this.P.S. Uncomment the
className
entries of the class definitions to enable caching of the_super
lookup. (The assumption is that these class names will be unique in the application.)如果你只想调用 this._super();不传递函数名称作为参数
最好使用这个插件:
https://github.com/lukasolson/Backbone-Super
If you want just to call this._super(); without passing the function name as an argument
Better use this plugin:
https://github.com/lukasolson/Backbone-Super
我相信你可以缓存原始方法(尽管未经测试):
I believe you can cache the original method (although not tested):
backbone._super.js,来自我的要点: https://gist.github.com/sarink/a3cf3f08c17691395edf
backbone._super.js, from my gists: https://gist.github.com/sarink/a3cf3f08c17691395edf
如果您不知道父类到底是什么(多重继承或者您想要一个辅助函数),那么您可以使用以下内容:
使用辅助函数:
In the case that you don't know what the parent class is exactly (multiple inheritance or you want a helper function) then you can use the following:
With helper function:
在实例化期间将父类作为选项传递:
然后在 MyModel 中,您可以像这样调用父方法
this.options.parent.method();
请记住,这会在两个对象上创建一个保留周期。因此,为了让垃圾收集器完成它的工作,您需要在完成其中一个对象后手动销毁它的保留。如果您的应用程序相当大。我鼓励您更多地研究层次结构设置,以便事件可以到达正确的对象。
Pass the parent class as an option during instantiation:
Then in your MyModel you can call parent methods like this
this.options.parent.method();
Keep in mind this creates a retain cycle on the two objects. So to let the garbage collector do it's job you would need to manually destroy the retain on one of the objects when finished with it. If you're application is pretty large. I would encourage you to look more into hierarchal setups so events can travel up to the correct object.
下面2个函数,一个需要你传入函数名,另一个可以“发现”我们想要哪个函数的超级版本
2 functions below, one requires you pass in the function name, the other can "discover" which function we want the super version of
我将这样做:
因此对于您的示例来说,这是:
Here is how I would do this:
so for your example this is: