开发者

What can I use Backbone JS's Models for exactly? Is this too far?

开发者 https://www.devze.com 2023-02-21 02:49 出处:网络
I have a question regarding Backbone JS\'s Models. I\'ve already delved into Backbone JS quite a bit the last few days. I am understanding it now (and thanks to Stackoverflow, I have understood furthe

I have a question regarding Backbone JS's Models. I've already delved into Backbone JS quite a bit the last few days. I am understanding it now (and thanks to Stackoverflow, I have understood further with a few things).

At present, my web app is using Backbone Models to store dynamic navigation and other such bits. Stuff that is generally used in Backbone Collections. At present, my web app is still needing to store sort of 'global' states... Such as what the user has currently selected in the navigation (which then effects other parts of the app etc.)

I've been keeping all of these stored 'states' in variables like this:

App.data.selectedPage = whatever etc.

But I'm thinking more about Backbone Models... Should I be storing these 'global states' in a Model? Is that something that the Models can be used for?

At present, the webapp doesn't save to the server, or local storage, it's more of a converted Flash presentation I have to code for an iPad. So it still made sense to use something like 开发者_Go百科Backbone for code organisation purposes... I mean, that's ok too right? To use Backbone, even I don't intend on storing the models anywhere?

Anyhow, yes, using models to store this sort of information is ok too? Anything goes? ...please do tell me if I'm approaching this wrong.

Many, many thanks. James


I think one good rule of thumb for using backbone model is if you need events when some data is changing.

For exemple, you have a calendar with a selected date. Many other parts (views) of your app needs to know and be informed about the selected date. Then it makes sense to store the date in a model and have everyone listen on events from the calendar.

This is more for data related functions. For state it is different. Your selected page is a state to me. Page selection, page state, globals, they should be in your controllers. They (or it) should know what the state of the page is and they can trigger event when it changes.

Model => data centric with events

0

精彩评论

暂无评论...
验证码 换一张
取 消