[英]Backbone.js - where to store state information?
I'm new to Backbone.js , and I'm trying to figure out where state variables should live . 我是Backbone.js的新手,我正在试图找出状态变量应该存在的位置 。 My use case:
我的用例:
I have an application that provides a reading interface for a book (I know, classic example, right?). 我有一个应用程序,为一本书提供阅读界面(我知道,经典的例子,对吧?)。 My models are
Book
and Page
with collection classes for each. 我的模型是
Book
和Page
,每个都有集合类。 The structure of the application looks roughly like this (forgive the ASCII visio): 应用程序的结构看起来大致如此(原谅ASCII visio):
+------------+
| Controller |
+------------+
| Views Models
| +--------------+ +----------------+
|-| IndexView |------| BookCollection |
| +--------------+ +----------------+
| |
| +--------------+ +----------------+
+-| BookView |------| Book |
+--------------+ +----------------+
| |
| +--------------+ |
|-| TitleView |-+ |
| +--------------+ | +----------------+
| +-| Page |
| +--------------+ | +----------------+
+-| PageView |-+
+--------------+
That is, the Controller
instantiates and coordinates two views, IndexView
and BookView
, backed by the models. 也就是说,
Controller
实例化并协调由模型支持的两个视图,即IndexView
和BookView
。 The BookView
instantiates and coordinates a set of subviews (there are actually more than shown here). BookView
实例化并协调一组子视图(实际上这里显示的不仅仅是这些)。
State information includes: 国家信息包括:
My question is, where should this state information live? 我的问题是,这个州的信息应该在哪里生活? Possible options include:
可能的选择包括:
The models , which could be state-aware. 这些模型可能是国家意识的。 This makes some sense, since they're intended to store data and views could listen for state changes, but it doesn't seem like this fits the intended Backbone.js pattern, and wouldn't always make sense (eg turning image on in the
PageView
should apply to all pages, not just the current one) 这是有道理的,因为它们旨在存储数据和视图可以监听状态更改,但它似乎不符合预期的Backbone.js模式,并且不会总是有意义(例如,打开图像
PageView
应该适用于所有页面,而不仅仅是当前的页面)
A special singleton model intended to hold state information. 用于保存状态信息的特殊单例模型 。 Again, makes sense, easy to listen to, all the views could bind to it - but again, this seems outside standard MVC.
再一次,有意义,易于倾听,所有视图都可以绑定它 - 但同样,这似乎超出了标准的MVC。
The views , who are responsible for the UI state - but this would require views to be aware of each other to get state info, which seems incorrect 的意见 ,谁负责的UI状态-但是这需要意见,了解彼此的获得状态信息,这似乎不正确
The controller , which should route the application between states - this makes sense, but it implies a slightly odd round trip, eg User selects "Show Images" --> View event listener is called --> View informs Controller --> Controller updates state --> Controller updates View
(rather than the simpler User selects "Show Images" --> View event listener is called --> View updates
) 控制器 ,它应该在状态之间路由应用程序 - 这是有道理的,但它意味着一个稍微奇怪的往返,例如
User selects "Show Images" --> View event listener is called --> View informs Controller --> Controller updates state --> Controller updates View
(而不是更简单的User selects "Show Images" --> View event listener is called --> View updates
)
I guess in some ways this is a generic MVC question, but I'm having trouble getting my head around it. 我想在某些方面这是一个通用的MVC问题,但我无法理解它。 What part of the application should be responsible for saving the current state?
应用程序的哪一部分应该负责保存当前状态?
UPDATE: For future reference, I used a global singleton State model for this problem. 更新:为了将来的参考,我使用全局单例状态模型来解决这个问题。 The UI flow goes like this:
UI流程如下:
app.State
app.State
什么都不做 app.State
- they're essentially specialized views that display and react to URL changes app.State
- 它们本质上是专门的视图,显示和响应URL更改 app.State
and update accordingly app.State
上的app.State
并相应地更新 My app is Open Source - you can see the code on Github . 我的应用程序是开源的 - 您可以在Github上看到代码 。 The relevant piece here is the State model , which I've extended to deal with (de)serializing state for the URL.
这里的相关部分是State模型 ,我已经扩展到处理(de)序列化URL的状态。
Don't limit your Backbone apps to Backbone constructs . 不要将Backbone应用程序限制为Backbone构造 。 If you find that your app needs some functionality that doesn't fit well into one of Backbone's constructs, don't shoehorn it into one just for the sake of keeping everything in Backbone.
如果您发现您的应用程序需要某些功能不能很好地适应Backbone的一个构造,请不要仅仅为了保留Backbone中的所有内容而将其整合到一个中。
I've found this backbone boilerplate to be helpful in this respect. 我发现这个骨干样板在这方面很有帮助。 It sets up modules for you and provides you with an app object that extends Backbone.Events (like in the previously-linked article).
它为您设置模块,并为您提供扩展Backbone.Events的应用程序对象(如之前链接的文章中所述)。 This app object can be used to store instantiated models, views and controllers/routers, and you should feel free to add your own non-backbone modules to the app object, to take care of responsibilities that don't fit perfectly into one of the Backbone constructs.
此app对象可用于存储实例化的模型,视图和控制器/路由器,您可以随意将自己的非主干模块添加到app对象,以处理不完全适合其中一个的责任。骨干构造。
Why don't create a state model to store and describe the current state? 为什么不创建状态模型来存储和描述当前状态? I don't think that is wrong.
我不认为那是错的。 Since the current state involves more than one model I think it's reasonable to create a state model to store and receive the current state.
由于当前状态涉及多个模型,我认为创建状态模型来存储和接收当前状态是合理的。
The controller could then communicate with the state model to get the current state. 然后,控制器可以与状态模型通信以获得当前状态。 The UI states should be stored in the corresponding model though.
UI状态应该存储在相应的模型中。 The state model knows which book and which page and then the book and page models keep track of their current UI states.
状态模型知道哪个书和哪个页面,然后书籍和页面模型跟踪他们当前的UI状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.