In the spine, I can use my models in different ways.
As I understand it, a data (data) model is used to store data (possibly from a RESTful web server), and the ViewModel is used to store information about a particular view (for example, hidden / displayed states).
Most of my knowledge is related to this SO issue.
After reading this article, where the author says:
Render UI after changing data, not user interaction
and
Stream: user interaction β data change β view rendering.
For example, if we write a play / pause switch button in an audio player, the stream will be:
- User gets into the game
- The state of the model (data) changes to βplayback
- View is displayed in playback mode
Following this pattern, you can switch this button to the correct position to change the state caused from other sources (for example, fast forward, new playlist, network error, etc.). In other words, we have one source of truth: whenever our model changes, we know that we need to display 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 began to wonder about the advantages and disadvantages of using each of them.
Assuming that we only have one model for each view (I know that we can have more, but if we restrict ourselves to one). I can think
ViewModel features:
- Those that are mentioned in the article.
- View information is stored in the model, preventing clogging of the view.
- Status information can be shared between views.
minuses:
- It is not possible to call the Backbone save () method, as this will cause the model to save incorrect data on the server (for example, the state of the views).
- It is not possible to call the getbone fetch () method because we can trample the data of our views.
(Data) Profi Page:
- Use the basic design created in save, select, etc. Representation
- can exchange data without worrying that certain data is stored in them.
minuses:
- Models can only be used for data.
Did I understand this correctly?
Should I have a model for my data and a model for my view?
How does it work with collections?
I know that the Beam is very free and there are no hard and fast rules. But does anyone have any real experience using one or the other? Or maybe both?
Any help is appreciated.