Question

getting to know backbone.js and the different templating systems I was wondering, what would be the best practice in giving objects to a template for rendering.

either pass in the whole backbone model and use <%= model.get('name') %>

or pass model.toJSON() and simply use <%= name %>

what are the pro / con's on using either one of these two methods? or can I choose what I find most useful without worrying about trade-offs?

Was it helpful?

Solution

it all comes down to what you want to do, really. most template systems allow you to pass any javascript object to the template and call the methods, properties and other bits on that object.

personally, i always pass the model.toJSON() to the template. but i know a few people who always pass the full model to the template and call .get like you're showing.

there's not much for tradeoffs. there's no performance penalty for doing either one. calling .toJSON() is such a fast call because it's just calling _.clone(this.attributes) behind the scenes. the benefit of passing toJSON() is that you don't have to write extra code in your template. you can just specify the properties directly. the benefit of passing the full model is that you can call methods on your object... but i would argue that this is a bad thing to do and that method calls should be done inside your view object, not the template... that's really a matter of opinion, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top