主干视图:从父级继承和扩展事件
Backbone 的文档指出:
事件属性还可以定义为返回事件哈希的函数,以便更轻松地以编程方式定义事件以及从父视图继承它们。
如何继承父级的视图事件并扩展它们?
父视图
var ParentView = Backbone.View.extend({
events: {
'click': 'onclick'
}
});
子视图
var ChildView = ParentView.extend({
events: function(){
????
}
});
Backbone's documentation states:
The events property may also be defined as a function that returns an events hash, to make it easier to programmatically define your events, as well as inherit them from parent views.
How do you inherit a parent's view events and extend them?
Parent View
var ParentView = Backbone.View.extend({
events: {
'click': 'onclick'
}
});
Child View
var ChildView = ParentView.extend({
events: function(){
????
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
一种方法是:
另一种方法是:
检查事件是函数还是对象
One way is:
Another would be:
To check whether Events is function or object
士兵.蛾的答案是一个很好的答案。进一步简化它,您可以执行以下操作
然后以典型方式在任一类中定义您的事件。
The soldier.moth answer is a good one. Simplifying it further you could just do the following
Then just define your events in either class in the typical way.
您还可以使用
defaults
方法来避免创建空对象{}
。You could also use the
defaults
method to avoid creating the empty object{}
.如果您使用 CoffeeScript 并将函数设置为
events
,则可以使用super
。If you use CoffeeScript and set a function to
events
, you can usesuper
.从 Backbone.View 创建专门的基本构造函数来处理层次结构中的事件继承不是更容易吗?
每当我们使用重新定义的扩展函数创建新的“子类”(子构造函数)时,这使我们能够减少(合并)层次结构中的事件哈希。
通过创建一个专门的视图:重新定义扩展函数的 BaseView,我们可以让想要继承其父视图声明的事件的子视图(如 AppView、SectionView)只需从 BaseView 或其派生之一进行扩展即可。
我们避免了在子视图中以编程方式定义事件函数的需要,在大多数情况下,子视图需要显式引用父构造函数。
Wouldn't it be easier to create specialized base constructor from Backbone.View that handles the inheritance of events up the hierarchy.
This allows us to reduce(merge) the events hash down the hierarchy whenever we create a new 'subclass'(child constructor) by using the redefined extend function.
By creating a specialized view: BaseView that redefines the extend function, we can have subviews(like AppView, SectionView) that want to inherit their parent view's declared events simply do so by extending from BaseView or one of its derivatives.
We avoid the need to programmatically define our event functions in our subviews, which in most cases need to refer to the parent constructor explicitly.
@soldier.moth 最后建议的简短版本:
Short version of @soldier.moth's last suggestion:
这也行得通:
直接使用
super
对我来说不起作用,要么手动指定ParentView
或继承类。访问
_super
var,该变量可在任何 CoffeescriptClass 中使用...扩展...
This would also work:
Using straight
super
wasn't working for me, either was manually specifying theParentView
or inherited class.Access to the
_super
var which is available within any coffeescriptClass … extends …
http://danhough.com/blog/backbone-view-inheritance/
http://danhough.com/blog/backbone-view-inheritance/
对于 Backbone 版本 1.2.3,
__super__
工作正常,甚至可以链接。例如:...在
A_View.js
中 - 将导致:For Backbone version 1.2.3,
__super__
works fine, and may even be chained. E.g.:... which - in
A_View.js
- will result in:我在这篇文章中找到了更有趣的解决方案< /a>
它使用了 Backbone 的 super 和 ECMAScript 的 hasOwnProperty。它的第二个进步例子就像一个魅力。这是一段代码:
您也可以对 ui 和 attributes 执行此操作。
此示例不考虑函数设置的属性,但本文的作者提供了这种情况下的解决方案。
I've found a more interesting solutions in this article
It use of the Backbone’s super and ECMAScript’s hasOwnProperty. The second of its progressives examples works like a charm. Here's a bit a code :
You can also do that for ui and attributes.
This example does not take care of the properties set by a function, but the author of the article offers a solution in that case.
要完全在父类中执行此操作并在子类中支持基于函数的事件哈希,以便子类可以不知道继承(如果子类重写,则子类必须调用 MyView.prototype.initialize )
初始化
):To do this entirely in the parent class and support a function-based events hash in the child class so that children can be agnostic of inheritance (the child will have to call
MyView.prototype.initialize
if it overridesinitialize
):这个 CoffeeScript 解决方案对我有用(并考虑了 @soldier.moth 的建议):
This CoffeeScript solution worked for me (and takes into account @soldier.moth's suggestion):
如果您确定ParentView将事件定义为对象,并且不需要在ChildView中动态定义事件,则可以通过以下方式进一步简化soldier.moth的答案:摆脱该函数并直接使用 _.extend :
If you are sure that the
ParentView
has the events defined as object and you don't need to define events dynamically inChildView
it is possible to simplify soldier.moth's answer further by getting rid of the function and using_.extend
directly:我喜欢的一种模式是修改构造函数并添加一些附加功能:
我更喜欢这种方法,因为您不必识别父级 - 少一个变量即可更改。我对属性和默认值使用相同的逻辑。
A pattern for this that I am fond of is modifying the constructor and adding some additional functionality:
I prefer this method because you do not have to identify the parent -one less variable to change. I use the same logic for
attributes
anddefaults
.哇,这里有很多答案,但我想我应该再提供一个。如果您使用 BackSupport 库,它会提供
extend2
。如果您使用extend2,它会自动为您合并事件(以及默认值和类似属性)。这是一个简单的示例:
https://github.com/machineghost/BackSupport
Wow, lots of answers here but I thought I'd offer one more. If you use the BackSupport library, it offers
extend2
. If you useextend2
it automatically takes care of mergingevents
(as well asdefaults
and similar properties) for you.Here's a quick example:
https://github.com/machineghost/BackSupport