Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 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

like image 870
littlejim84 Avatar asked Jan 21 '23 03:01

littlejim84


1 Answers

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

like image 133
Julien Avatar answered Jan 24 '23 00:01

Julien