JavaScript MVC:视图如何通知控制器
如果我在 JavaScript 中遵循粗略的 MVC 模式,那么视图(例如 button
元素)通知控制器的最佳方式是什么?
按钮
是否应该触发控制器必须监听的事件?或者,按钮
应该直接调用控制器函数吗?或者也许控制器应该将事件分配给视图?
感谢您的任何意见!
If I'm following a rough MVC pattern in JavaScript, what is the best way for the view (such as a button
element) to notify the controller?
Should the button
fire an event that the controller has to listen to? Or, should the button
call a controller function directly? Or maybe the controller should assign the event to the view?
Thanks for any input!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想说的是,视图应该捕获按钮触发的事件,并触发其自己的事件,该事件将由控制器处理。
让我解释一下:
@raynos 写道:
尽管我同意第一个陈述,但我个人不喜欢这种解释
遵循此声明意味着控制器必须了解 UI 中的每个按钮/文本字段/元素及其 ID/选择器。
我更喜欢让 View 触发语义事件,例如“languageSelected”或“searchRequested”,并将相关数据添加到事件中。
因此,典型的流程是视图呈现一些 UI(假设它有一个搜索框和一个按钮),当用户单击按钮时 - View 处理该事件并触发其自己的“searchRequested” “ 事件。此事件由控制器处理,该控制器将调用模型要求其执行搜索。完成后,模型将触发“searchResultsUpdated”evnet,该事件将由视图处理,使其显示结果。
如果您现在选择更改应用程序的设计以显示搜索词链接而不是搜索框和按钮(或者即使您并排 - 或在不同的屏幕上),您唯一需要更改的是 <强>查看。
技术旁注:
如果使用 JQuery 并假设您的视图是一个 javascript 对象,您可以使用它
来触发事件
并
侦听和处理事件。
I would say that the View should catch the event fired by the button and fire its own event that will be handled by the controller.
Let me explain:
@raynos wrote:
personally even though I agree with the first statement I don't like the interpretation.
To follow this statement means that the controller has to know of every button/text field/element in the UI and its ID/Selector.
I prefer to have the View fire semantic events such as "languageSelected" or "searchRequested" and add the relevant data to the event.
so a typical flow would be that the View renders some UI (lets say it has a search box and a button), when the user clicks the button - the View handles the event and fires its own "searchRequested" event. This event is handled by the Controller that would call the Model asking it to perform the search. when done, the Model will fire a "searchResultsUpdated" evnet which will be handled by the View causing it to show the results.
if you now choose to change the design of your app to show search term links instead of a search box and a button (or even if you have then side by side - or on different screens) the only thing you need to change is the View.
A technical side-note:
If using JQuery and assuming your view is a javascript object you can use
to fire the event
And
to listen for and handle the event.
有趣的问题。我认为这在很大程度上取决于您的情况、示例的复杂性以及您正在使用的特定 JavaScript 模式。
如果您正在谈论的按钮只是一个 HTML 元素,这可能是一个简单的方法:
或者,如果您的
button
是您创建的对象或模块,您可以设置回调:不幸的是,简单的例子并不能真正说明不同方法的好处!
Interesting question. I think it would depend quite a lot on your situation, the complexity of your example, and the particular JavaScript patterns that you're using.
If the button you're talking about is simply an HTML element, this might be a simple way:
Or, if your
button
is an object or module that you've created, you could set a callback:Unfortunately, trivial examples don't really illustrate the benefits of different approaches!
有很多方法可以做到这一点。这是一种方法。
There are many ways to do it. Here is one way.