主要成分:是真的视图控制器? [英] Backbone: Are Views really Controllers?

查看:85
本文介绍了主要成分:是真的视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设我的第一个Backbone.js的应用程序,我很困惑,我多少责任应该给或者从我的观点隐藏。

I'm building my first Backbone.js app and I'm confused about how much responsibility I'm supposed to give to or hide from my Views.

在我的例子中,我构建的动态地从集合生成的丰富的UI表(类似YUI的数据网格)。在我的应用我把这叫做AppTable。在我的MVC的理解,我会想象那里会是​​某种AppTable控制器其中找到正确的收集,抓住一个哑查看并传递给视图从它需要呈现集合任何信息。在这一塞纳里奥,视图会做多一点不是采取向它提供的数据,并相应修改DOM,甚至填充模板或附加事件侦听器。

In my example, I'm building a Rich UI Table (similar to YUI's datagrid) that's dynamically generated from a Collection. In my app I call this an "AppTable". In my understanding of MVC, I would imagine that there'd be some kind of AppTable controller which finds the correct Collection, grabs a "dumb" View and passes to the View whatever information from the Collection it needs to render. In this senario, the View would do little more than take the data provided to it and modify the DOM accordingly, maybe even populating a template or attaching event listeners.

主干似乎与具有控制器的查看和收集之间进行调解的想法做掉。而不是一个视图获取与一个集合的引用进行初始化,这是查看的责任进行自我更新。

Backbone seems to do away with the idea of having a controller mediate between the View and Collection. Instead a View gets initialized with a reference to a Collection and it is View's responsibility to update itself.

我是正确认识这个架构?

Am I understanding this architecture correctly?

假如我这样做,我的问题就变成了,当我的视图需要做更多,会发生什么?例如,我想列排序,拖和下降的行,分页,搜索,表控制环节(如新,复制,删除行...等),等等。如果我们坚持与其中的观点是直接连接到一集聪明的景观模式,做上述功能结缘视图对象?

Assuming I do, my question then becomes, what happens when my View needs to do more and more? For example, I want column sorting, drag-and-drop for rows, pagination, searching, table control links (like new, copy, delete row... etc), and more. If we stick with a "smart" View paradigm where the View is connected directly to a Collection, do the above functions become attached to View object?

通过这个思路,我可以看到鉴于从简单的表包装器pretty凌乱野兽很多连接到它的功能越来越多。那么,在这种情况下,查看一个真正的控制器?

Thinking through this, I could see the View growing from a simple table wrapper to pretty messy beast with lot of functionality attached to it. So, is the View really a controller in this case?

推荐答案

您对建筑的理解是正确的。骨干网不承认传统的MVC意义上的控制者的概念。 (事实上​​,骨干网用于实际有一个对象叫做控制器,但它已被重新命名为路由器更准确地描述它做什么。)

Your understanding of the architecture is correct. Backbone does not recognize the concept of a "controller" in the traditional MVC sense. (In fact, Backbone used to actually have an object called a Controller, but it has been renamed Router to more accurately describe what it does.)

在列表中的功能(拖放,删除行,排序等)将在视图中都属于。视图描述你所看到的,并响应用户输入。任何涉及事件(点击,一键preSS,一个提交等)都去一个视图内。但是,你的观点不应该实际操作数据;应该由它的模式来完成。你是在想,一个视图就像一个控制器正确的,因为它打包数据并将其发送到模型,然后将验证/套/保存适当。一旦这些操作已经发生,则视图重新呈现本身重新present模型内的数据的新版本。

The functions you list (drag-drop, delete rows, sorting, etc.) would all belong in a View. A view describes what you see and responds to user input. Anything involving an event (a click, a keypress, a submit, etc.) all go inside of a view. But your view should never actually manipulate the data; that should be done by its model. You are correct in thinking that a view acts like a controller, because it packages data and sends it to the model, which will then validate/set/save appropriately. Once those actions have occurred, the view re-renders itself to represent the new version of the data inside the model.

一个值得注意的问题:你的观点不应该太吃力地绑在DOM。它是骨干约定,有你的看法是联系在一起(例如,窗体或格),然后只用它的子元素处理的顶级DOM元素。这是合适的;一般来说,像删除此链接,从这个div你的看法里面都没有。如果你发现你的看法越来越笨拙,你最有可能需要将其分解成子视图,各有各自的行为,因为他们整个的组成部分。

One note of caution: your view should not be too strenuously tied to the DOM. It is Backbone convention to have a top-level DOM element that your view is tied to (e.g., a form or a div) and then deal only with its sub-elements. That is appropriate; in general, things like "remove this link from this div" inside your view are not. If you find your view growing unwieldy, you most likely need to break it into subviews, each with their respective behaviors as components of their whole.

这篇关于主要成分:是真的视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆