I'm new to Backbone.js, and I'm trying to figure out where state variables should live. 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. The structure of the application looks roughly like this (forgive the 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. The BookView
instantiates and coordinates a set of subviews (there are actually more than shown here).
State information includes:
- the current book (pointer or id)
- the current page (pointer or id)
- other UI state variables, such as whether images on the page are visible or not, whether other widgets are open or closed, etc.
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 (e.g. turning image on in the
PageView
should apply to all pages, not just the current one)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.
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
The controller, which should route the application between states - this makes sense, but it implies a slightly odd round trip, e.g.
User selects "Show Images" --> View event listener is called --> View informs Controller --> Controller updates state --> Controller updates View
(rather than the simplerUser 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. 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:
- View UI handlers do nothing but update
app.State
- My routers also do nothing but update
app.State
- they're essentially specialized views that display and react to URL changes - Views listen to changes on
app.State
and update accordingly
My app is Open Source - you can see the code on Github. The relevant piece here is the State model, which I've extended to deal with (de)serializing state for the URL.