MVC 模式 - 将事件监听器从控制器绑定到视图元素上?
如何将事件侦听器从控制器绑定到视图的元素,例如按钮(单击事件)到其自己的处理程序?
最初我是从视图中这样做的,例如。
button.addEventListener(MouseEvent.CLICK, controller.buttonClick);
但现在意识到这是错误的,因为阅读“每个视图只应该“知道”它所代表的模型,而“不知道”控制器”
How do you bind an event listener from the controller to the view's elements eg button (click event) to its own handler?
Originally I was doing this from the view eg.
button.addEventListener(MouseEvent.CLICK, controller.buttonClick);
But now realise this is wrong since reading "each view is only supposed to "know" about the model which it represents, and "know" nothing of the controller"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模型应该有此按钮的实例,因此控制器将访问模型以添加事件侦听器,但视图将仅显示此按钮,仅添加到舞台。
model should have instance of this button, so controller will access model to add event listener, but view will only show this button, only add to stage.
控制器可以直接引用视图,因此可以将事件侦听器绑定到适当的视图元素。如果您愿意,View 可以公开公共方法来设置绑定。
Controller has direct reference to the view so can bind event listeners to the appropriate view elements. View can expose public methods to set bindings if you like.