父子视图事件绑定关系
我想知道亲子关系中的事件应该设置在哪里。这不是一个具体问题,只是我猜的最佳实践。我的具体情况:
我有一个 Dropdown
,其中包含一个 Button
和一个 List
。描述操作后,按下按钮,就会出现列表。单击列表中的某个项目,该项目就会被选中,列表就会消失。
因此,第一件事是,由于视图被初始化为 Dropdown
,因此制作它的人不需要接触并处理 Button
或 List
视图。所有方法都应该在父视图上调用,并且事件可能需要从子视图中冒泡。
例如,Dropdown
提供了自己的 press
方法,该方法仅调用 < code>Button 的按下方法。
当用户按下按钮时,Button
会触发 Dropdown
正在监听的 press
事件。
onButtonPress : function () {
if (!this.isExpanded()) {
this.expand();
}
this.trigger('press');
},
并且 Dropdown
会触发自身按下,这样开发人员无需访问 dropdown.button
即可获取按下事件。
这就是第一个问题。 Dropdown
应该在 onButtonPress
中扩展自身,还是应该 onButtonPress
仅触发按下,然后让扩展侦听 Dropdown
自己的 press
事件:
onButtonPress : function () {
this.trigger('press');
},
onPress : function () {
if (!this.isExpanded()) {
this.expand();
}
},
然后事情变得更复杂,如果 Dropdown
的 expand
方法只触发 expand
就其本身而言:
expand : function () {
if (this.isEnabled()) {
this.setState('expanded', true);
this.trigger('expand');
}
return this;
},
onExpand : function () {
this.list.show();
},
或者应该是显示
是列表
:
expand : function () {
if (this.isEnabled()) {
this.setState('expanded', true);
this.list.show();
this.trigger('expand');
}
return this;
},
我想我只是想知道决定在父/子关系中绑定事件的最佳实践。如何避免混乱的情况和可能的循环事件调用。
有人有什么想法吗?
I'm wondering about where events should be set in a parent-child relationship. It's not a specific problem, just a best practices I guess. My specific case:
I have a Dropdown
which contains of a Button
and a List
. Describing the action, you press the button and the list appears. You click an item in the list, and that item is selected and the list disappears.
So the first thing is that since the view is initialized as a Dropdown
, the person making it shouldn't need to reach in and deal with the Button
or List
view. All methods should be called on the parent view, and events might need to be bubbled up from the child.
So for example, instead of doing: this.dropdown.button.press();
, Dropdown
provides its own press
method which just calls Button
's press method.
When the user presses the button, Button
fires a press
event that Dropdown
is listening to.
onButtonPress : function () {
if (!this.isExpanded()) {
this.expand();
}
this.trigger('press');
},
And Dropdown
triggers press on itself, so that developer can get the press event without reaching in to dropdown.button
.
Here's where the first question comes. Should Dropdown
expand itself in onButtonPress
or should onButtonPress
just trigger a press, and then have the expansion listening on Dropdown
's own press
event:
onButtonPress : function () {
this.trigger('press');
},
onPress : function () {
if (!this.isExpanded()) {
this.expand();
}
},
And then it gets more complicated, should Dropdown
's expand
method just trigger expand
on itself:
expand : function () {
if (this.isEnabled()) {
this.setState('expanded', true);
this.trigger('expand');
}
return this;
},
onExpand : function () {
this.list.show();
},
or should it be the one that show
s the List
:
expand : function () {
if (this.isEnabled()) {
this.setState('expanded', true);
this.list.show();
this.trigger('expand');
}
return this;
},
I guess I'm just wondering about best practices for deciding where to bind events in a parent/child relationship. How to avoid confusing situations and possibly circular event calling.
Anyone have any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会简单地使用 DOM 来实现此目的,而不是手动触发事件。如果您希望事件首先命中父级,则将捕获设置为 true,如果您希望事件冒泡,则将捕获设置为 false。
这允许在捕获阶段调用事件处理程序,因此父 DOM 元素首先获取事件:
此冒泡,因此子级首先接收事件,并且当子级接收事件时调用处理程序。
一般来说,保持简单。您还可以使用委托编程模式,其中父级将获取其自身和子级的事件,这样您就可以避免混乱,并可能节省较大应用程序的内存。
I would simply use the DOM for this instead of triggering an event manually. If you want to have an event hit the parent first, then set the capture to true, or false if you want the event to bubble.
this allows an event handler to be called during the capture phase, so parent DOM elements get event first:
this bubbles, hence children receive event first, and handler is called when children receive event.
In general keep it simple. You can also use the delegation programming pattern where the parent will get the events for itself and the children, so you can avoid confusion and potentially can save memory with larger applications.