I am building a small Backbone.js application and added some custom getters to one of the models (the name getter returns a concatenated first- and last name for example):
PersonModel = Backbone.Model.extend({
get: function (attr) {
if (typeof this[attr] == 'function') {
return this[attr]();
}
return Backbone.Model.prototype.get.call(this, attr);
},
name: function() {
return firstName + " " + lastName;
}
})
I can now use person.get("name") to retrieve the name, nice. However, when I call toJSON on the the model these values aren’t included (and I suppose that makes sense). Problem is I use this to render my views:
this.template({people: this.collection.toJSON()});
What’s the best way to do this in Backbone.js? Manually creating the JSON with the overwritten getters?
Thanks!
You could provide your own
toJSONmethod on PersonModel:The collection toJSON just calls
toJSONon each model:so adding your own
toJSONto your model should work.You could also add
nameas a real attribute and then adjust your model’svalidatemethod to updatenameiffirstNameorlastNamechanges and to ignore any direct attempts to changenameor complain about “an attempt to edit a read-only attribute” when someone tries to pass anamechange toset. There’s nothing that says thatvalidatecan’t change the attribute object that it is given so you could be given{firstName: 'x'}and change it to{firstName: 'x', name: 'x ' + this.get('lastName')}beforevalidatereturns. This would be a bit of an abuse ofvalidatebut there is no explicit prohibition againstvalidatealtering the attribute set and it is the only hook you have. I suppose you could have the model listen to change events on its ownfirstNameandlastNameand then trigger aset({name: ...})but then you could have event ordering problems if someone else is watching only the first and last names.