简体   繁体   English

backbone.js - 视图内的视图和管理事件

[英]backbone.js - views within views and managing events

What's a good way to organize views? 组织观点的好方法是什么? Let's say I have a div that will contain a view from an admin panel perspective of users - there will be a list of users along with options to choose how many to display at a time, sorting options, what page to be on, filters, etc... 假设我有一个div,它将包含来自用户管理面板视角的视图 - 将会有一个用户列表以及选择一次显示的数量,排序选项,要打开的页面,过滤器,等等...

Would I want an outside view that contained everything except the table and data? 我想要一个包含除表和数据之外的所有内容的外部视图吗? And then an inside view that contains the table (along with the data)? 然后是一个包含表格的内部视图(以及数据)? And would the pagination have it's own view? 分页会有它自己的看法吗? And how would the pagination view use the click event to update the user view? 分页视图如何使用click事件更新用户视图? I'm just confused on how to organize the views while still being able to have different events trigger other views to render() / collections to fetch() . 我只是对如何组织视图感到困惑,同时仍然可以让不同的事件触发其他视图来render() /集合到fetch()

So a basic hierarchy would look like: 所以基本层次结构如下:

- User View
  - Table
    - List of Users
  - Pagination
    - List of available numbers to click
  - Filters
    - Possible filters to apply to the data

Yet clicking a filter or number in the pagination should be able to get the collection to fetch() new data and refresh the view; 然而,在分页中单击过滤器或数字应该能够使集合fetch()新数据并刷新视图;

I second dogenpunk. 我第二次做事。 I would have one User Collection / View. 我会有一个用户集合/视图。 Because the whole hierarchy you describe above is about that one User Collection. 因为您在上面描述的整个层次结构是关于那个用户集合的。 All functions of it manipulate that collection and then you rerender the User View. 它的所有功能都操纵该集合,然后您重新呈现用户视图。

You could have a second User View, one single User, tied to a Model if you want to apply changes to the server for that user only. 如果要仅为该用户将更改应用于服务器,则可以将第二个用户视图(单个用户)绑定到模型。

My 2 cents to your original question. 我原来问题的2美分。 If you really want to make it a MV*, a pagination would be a view, your table would be a view. 如果你真的想让它成为MV *,那么分页就是一个视图,你的表就是一个视图。 And have your collection to send out ( trigger ) events to change your view. 让你的收藏发送(触发)事件来改变你的观点。 And another questions I would ask myself also is what will be affected when my collection changes? 我会问自己的另一个问题是,当我的收藏改变时会受到什么影响? For example, in your case, I don't think the collection changes will affect your userView directly, it only affects the Table and Pagination. 例如,在您的情况下,我不认为集合更改会直接影响您的userView,它只影响表和分页。

I try to reflect my server-side MVC structure as much as I can. 我尽可能多地反映我的服务器端MVC结构。

Everything that can be put into a plugin, I do so, and then I keep those plugins in a separate location to the controllers which call the plugins. 可以放入插件的所有内容,我这样做,然后我将这些插件保存在调用插件的控制器的单独位置。 So in your case, the table view for the list of users would be held either in a table plugin, or possibly in the 'users' module if it was code I was really only going to use once. 因此,在您的情况下,用户列表的表视图将保存在表插件中,或者可能保存在“用户”模块中,如果它是我实际上只会使用一次的代码。

If I need to override the output of the plugin, then I store the view inside the module folder. 如果我需要覆盖插件的输出,那么我将视图存储在模块文件夹中。

What I try to avoid doing is storing views purely by the type of HTML inside them, so I wouldn't store a module's view as 'table' because that will get confusing if later it changes to a list. 我试图避免做的是纯粹根据其中的HTML类型存储视图,因此我不会将模块的视图存储为“表”,因为如果稍后它更改为列表将会混淆。 Obviously, if I have a 'table' plugin then the view for that is going to be a table, but then changing the JavaScript view means just changing the plugin call from 'table' to 'list' anyway. 显然,如果我有一个'table'插件,那么该视图将成为一个表,但是更改JavaScript视图意味着只需将插件调用从'table'更改为'list'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM