我正在使用Parse和主干,我尝试从Parse模型中打印一个值列表。这很简单,但我对objectId有问题。
那我有..。
var Thing = Parse.Object.extend('MyThings');
var Things = Parse.Collection.extend({
model: Thing
});
var collection = new Things();
collection.fetch({
success: function(){
App.start(collection.toJSON());
}
});这是风景..。
ThingView = Backbone.View.extend({
tagName: 'tr',
render: function(){
this.$el.html(_.template($('#item-template').html(), this.model.attributes));
}
});
ThingListView = Backbone.View.extend({
tagName: 'table',
addAll: function(){
this.collection.forEach(this.addOne, this);
},
render: function(){
this.$el.empty();
this.addAll();
},
addOne: function(item){
var itemView = new ThingView({model: item});
itemView.render();
this.$el.append(itemView.el);
}
});这是一个模板(用Underscore.js).
<script type="text/template" id="item-template">
<td><%= id %></td>
<td><%= name %></td>
<td><%= color %></td>
</script>属性名称和颜色显示正确,但“id未定义”
有什么想法吗?
发布于 2014-11-28 17:50:15
问题是id不存在于属性中。它实际上生活在物体的根部。因此,您需要做的是将this.model传递给_.template()而不是this.model.attributes,然后在HTML中进行相应的调整--如下所示:
render: function(){
this.$el.html(_.template($('#item-template').html(), this.model));
}
<td><%= id %></td>
<td><%= attributes.name %></td>
<td><%= attributes.color %></td>https://stackoverflow.com/questions/26608608
复制相似问题