In backbone I can use my models in different ways.
As I understand, a (Data) model is used to store data (possibly served from a RESTful web server) and a ViewModel is used to store information about a specific view (The views hidden/shown state for example).
Most of my knowledge is from this SO question.
After reading this article where the author says:
Render UI Upon Data Change, Not User Interaction
and
The flow is : User interaction -> data change -> view render.
For instance, if we are writing a play/pause toggle button in an audio player, the flow would be:
- The user hits ‘play’
- The model (data) state changes to ‘playing’
- The view renders in ‘playing’ mode
Following this pattern ensures that changes to the state triggered from other sources (such as fast forward, new playlist, network error, etc.) will toggle our button to do the right thing. In other words, we have a single source of truth: whenever our model changes we know we should render our button.
var PlayPauseButton = Backbone.View.extend({
tagName: 'li',
className: 'icon',
initialize: function(){
this.model.on('change:status', this.render, this);
},
render: function(){
this.$el.removeClass('icon-play').removeClass('icon-pause');
this.$el.addClass('icon-' + (this.isPlaying() ? 'pause' : 'play'));
},
events: {
'click': function(){
this.model.set('status', this.isPlaying() ? 'paused' : 'playing');
}
},
isPlaying: function(){
return this.model.get('status') === 'playing';
}
});
I started to wonder the advantages and disadvantages of using each one.
Assuming we only have one model per view (I know we could have more, but if we limit it to one). I can think of,
ViewModel pros:
cons:
(Data) Model pros:
cons:
Am I correct in thinking this?
Should I have a model for my data and a model for my view?
How does this work with collections?
I know Backbone is very loose and there are no hard and fast rules. But, does anybody have any real world experience of using one or the other? Or possibly both?
Any help is appreciated.
You're actually assuming you can't use your Data(Domain) models in the back end and that you need to deal with your data as ViewModel objects. The way I've been taught to do it by seasoned professionals of the Stack Overflow realm is:
1.) Always use ViewModels for views. Don't use Domain models. Domain models are often not completely suitable for views and this leads to people using magic strings, request caching and even sessions to store view-specific information, all of which is unnecessary.
2.) Due to this limiting you in the ways you've pointed out such as not being able to use data model methods built into Backbone, use an object mapper, either of your own creation or one someone else made earlier, to map the properties of the ViewModel to the properties of the Data model when you need to use it.
This allows you to have highly specific ViewModels without needing to worry about not being able to use Backbone's own Data models.
Regarding your slightly separate question about collections, you should store collections of ViewModel objects for other things you need. For example, if you have a list of Cars, you should provide your CarListViewModel with a list of CarViewModel objects. Should you choose to use Data objects when you get this far down the ladder of models it has lower impact, but should still be avoided.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With