JavaScript 中的被动视图
我正在考虑在 JavaScript 中实现 MVP - 被动视图模式。在大多数情况下,视图将是简单的 dom 元素,其中演示者附加事件侦听器。但是,当涉及到像基于 JavaScript 的伪选择框、自动建议或 aria 功能之类的小部件时,这应该是 JavaScript 视图类的一部分,还是应该将更改视图的逻辑作为演示者的一部分? 我查看了 javascriptMVCs 视图,看来它们只是生成的模板没有任何逻辑的html。但对我来说,监听 html 选择框的演示者和监听带有 javascript 逻辑来模仿真实选择框的伪选择框的演示者似乎没有什么不同。两者都不应该关心盒子内部如何工作,他们只需要监听更改事件。
那你觉得怎么样呢。视图类的工作是什么。
I'm thinking about an implementation of MVP - Passive View pattern in JavaScript. In most case the view will be simple dom elements where the presenter attaching event listeners. But when it comes to widgets like JavaScript based pseudo selectboxes, auto suggest or aria features, should this be part of a JavaScript view class or should the logic to change the view be part of the presenter?
I've looked at the javascriptMVCs view and it seems that they are only template generated html without any logic. But for me there seems no different between a presenter who's listenen to html selectbox and one who listenen to an pseudo selectbox with javascript logic to mimic a real select box. Both shouldn't care about how the box is working internally, they just have to listen the change event.
So what to you think. Whats the job of view class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
View 对象应该负责绘制自身。被动观点知道如何做事,而不是做什么。它必须负责触发自己的事件。仅仅因为它是一个自定义选择框小部件,并不意味着它与常规选择框有任何不同。
如果演示者接管了所有视图职责,则根本不需要视图。最好只有一个演示者。
The View object should be responsible for drawing itself. A Passive View knows how to do stuff, not what to do. It must be responsible for firing its own events. Just because it is a custom select box widget does not make it any different than a regular select box.
If the presenter took over all the views responsibilities, then there is no need for a view at all. Might as well just have a presenter only.
视图的工作是作为视图技术的适配器。在这种情况下,您的视图技术可能是通过 JavaScript 实现的 jQuery over HTML。该视图应设计为执行三件事:
被动视图模式中的视图无状态。我再说一遍,视图并不代表状态。我的意思是,所有状态和状态转换的知识都由呈现者表示。视图只是在演示者和实际视图技术之间提供间接连接的粘合剂。 除了上面的三个要点之外,它没有封装任何如何处理它所包含的信息的知识。这很重要,因为您永远不应该期望视图处于一致状态。您应该始终让演示者负责管理视图的状态。
上面的堆栈演示了我如何预见模型、视图和演示者的交互,以及它们与视图技术和控制器。
The view's job is as an adapter to the view technology. In this case, your view technology is likely jQuery over HTML via JavaScript. The view should be designed to do three things:
A view in the passive view pattern is not stateful. I repeat, a view does not represent state. What I mean by this is that all state and knowledge of state transition is represented by the presenter. The view is simply the glue that provides indirection between the presenter and the actual view technology. Beyond the three bullet points above, it does not encapsulate any knowledge of what to do with the information it contains. This is important, because you should never expect a view to be in a consistent state. You should always hold the presenter responsible for managing the state of the view.
The stack above demonstrates how I foresee the model, view, and presenter interacting, as well as their relationship to the view technology and the controller.
JavaScriptMVC 的视图是虚拟的客户端模板。它的节点控制器倾向于承担更传统的视图角色。
JavaScriptMVC's views are dummy client side templates. It's node controllers tend to take on a more traditional View role.