3 ms·
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
by rgarcia 15y ago
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...
- jondot 15y agoThat'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-one-model-selected-in-a-backbone-js-collection http://stackoverflow.com/questions/6519990/best-way-to-make-...
- finpingvin 15y agoYup, can simplify code for wiring together views a great deal and promote decoupling between them.
- bricestacey 15y agoI 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')
- finpingvin 15y agoBut if you're wiring together several views dependent on the selected state, this type of code can lead to spaghetti and tight coupling between views.
- bricestacey 15y agoThis 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?
- jondot 15y agoIt 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 http://martinfowler.com/eaaDev/PassiveScreen.html