To make it simple, should I do
$(@el).html @template(model: @model)
or
$(@el).html @template(@model.toJSON())
I used to go the first way, that’s what I was taught. But recently I found second way is acceptable too. It at least saved a lot @model.get(“attribute_name”) while rendering templates.
So what’s the best way?
I think
@model.toJSON()is better, for the reason you’ve mentioned: it makes for cleaner templates.This:
Is easier on the eyes than:
Look at some of the example apps on Backbone.js, you’ll see they use the
.toJSON()approach.I suppose it’s also a question of whether you want your templates to have access to the full View object. Obviously, that is not possible once you use
toJSON(). To me, not having that access is a plus respecting the separation of concerns, since the template should be about presentation, with minimal code, and appearing as close to regular HTML markup as possible.