开发者

passing the backbone model or an exported json object to a templating system like mustache

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜