Not a backbone expert, but how "correct" is it to have a presentation variable like "selected" tied to a backbone model? For some reason the OP's inclusion of a "selected" attribute on his model struck me as odd. I don't think you'd want to sync this with the backend...
That's a good question. If you think of a model representing whats behind a view, in a non-web world (stateful), selectedness can very well sit in a model - as long as it represents a state of the view.
In this example, the selectedness sits in the collection, however it may sit in the view. See more about this here, for example:
http://stackoverflow.com/questions/6519990/best-way-to-make-...
I would have probably not stored the state of selectedness and left it to the DOM. In order to remove the selectedness state before setting the new selected image I'd just do something like this. $('div ul li img.selected').removeClass('selected')
This is supposed to be an introductory tutorial on Backbone. That should not be a concern. Second, the author makes the claim that Backbone is an MVC and makes things simpler and yet violates that. His way you literally have to go through the view, the collection, the model, a trigger, and back to the view to select a new image. WTF?
It simply is a matter of concerns. Ideally, your model should represent the state of your application, it doesn't matter if an event needs to travel one or more layers in order for that to happen.
Views may very well be a representation of the model.
Although the MV* stack is packed with examples (MVC, MVP, MVVM), here is one example ('Passive View'): http://martinfowler.com/eaaDev/PassiveScreen.html
7 comments
[ 4.0 ms ] story [ 31.0 ms ] thread