i have a backbone view that renders a model to screen. When the model (IsNew) i have to get my distance value out of this.model.attributes.distance otherwise out of this.model.distance
Why is that?
So Somewhere in the code I have this:
if(!this.model.distance)
distance = this.model.attributes.distance
else
distance = this.model.distance;
It is an ugly solution in my opinion and there is probably an nicer way of doing this.
Any clues?
You’re likely getting and setting your values incorrectly.
To set a value:
this.model.set({name: "value"})To get a value:
this.model.get("value")the
setmethod takes in a JSON document as the parameter and allows you to specify multiple attributes to assign:this.model.set({foo: "bar", baz: "quux"})The
getmethod only allows you to specify one value to get, by name.By going directly to the model.attributes, you’re circumventing all of the logic and process that Backbone puts in place for models, including the event notifications and validation. It’s recommended that you use the
setandgetmethods instead of directly accessing the attributes.