When making a backbone Model, I am going to get into many situations in which I will want to get, and set values for various attributes in the attributes object of a backbone Model. Getting values is not a big deal as the model.get(‘foo’) method is just a shorthand for model.attributes.foo. However it is important to use the set method to set values rather than directly modifying the attributes object so that any events that have been defined will work. In this post I will be showing some simple examples of the set model method in backbone.
So to outline why it is important to use set lets start with a real simple example of a backbone Model. In addition lets make a single instance of that Model, and define an event handler for it.
The problem with just setting the value by directly changing the contents of attributes object is that no other code defined in backbones set method will run. As such any events that should fire will not fire.
This all that comes to mind for now on this matter. Be sure to check out my many other growing posts on backbone if you feel inclined.