【发布时间】:2011-11-26 02:45:44
【问题描述】:
我对主干 js 和 Mustache 还是很陌生。我正在尝试从 rails json 对象加载页面加载的主干集合(对象数组)以保存额外的调用。我在使用 mustache 模板渲染骨干集合时遇到了麻烦。
我的模型和收藏是
var Item = Backbone.Model.extend({
});
App.Collections.Items= Backbone.Collection.extend({
model: Item,
url: '/items'
});
并查看
App.Views.Index = Backbone.View.extend({
el : '#itemList',
initialize: function() {
this.render();
},
render: function() {
$(this.el).html(Mustache.to_html(JST.item_template(),this.collection ));
//var test = {name:"test",price:100};
//$(this.el).html(Mustache.to_html(JST.item_template(),test ));
}
});
在上面的视图渲染中,我可以渲染单个模型(注释测试对象),但不能渲染集合。我完全被打动了,我尝试过下划线模板和小胡子,但没有运气。
这是模板
<li>
<div>
<div style="float: left; width: 70px">
<a href="#">
<img class="thumbnail" src="http://placehold.it/60x60" alt="">
</a>
</div>
<div style="float: right; width: 292px">
<h4> {{name}} <span class="price">Rs {{price}}</span></h4>
</div>
</div>
</li>
我的对象数组看起来像这样
【问题讨论】:
-
你能发布你的小胡子模板吗?
-
@DerickBailey,添加了信息,请查看..
标签: javascript jquery templates backbone.js mustache