【发布时间】:2012-04-07 16:23:29
【问题描述】:
我从我拥有的宁静服务中得到了这个结果:
注意:响应是 JSON 格式,它是来自 chrome 的插件,它会这样显示。
如果你看图二[上图],模型属性是 Items,那么每个项目都在 Items 下。我应该怎么做才能访问item?
我的问题是我无法从此结果中访问或检索每个项目的数据。我不能从服务器端改变任何东西。我正在使用带有此代码的主干。
window.Item = Backbone.Model.extend();
window.ItemCollection = Backbone.Collection.extend({
model: Item,
url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials'
});
window.ItemListView = Backbone.View.extend({
tagName : 'ul',
initialize: function(){
this.model.bind("reset",this.render,this);
},
render: function(eventName){
_.each(this.model.models.Items, function(item){
$(this.el).append(new ItemListItemView({model:item}).render.el);
},this);
return this;
}
});
window.ItemListItemView = Backbone.View.extend({
template : _.template($("#item-list").html()),
render: function(eventName){
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
var AppRouter = Backbone.Router.extend({
routes:{
"":"list"
},
list:function(){
this.itemList = new ItemCollection();
this.itemListView = new ItemListView({model:this.itemList});
this.itemList.fetch();
$("#itemContainer").html(this.itemListView.render().el);
}
});
var app = new AppRouter();
Backbone.history.start();
更新
我能够纠正嵌套 json 对象的问题。现在 Models 属性或我的 Collection 填充了单个项目。但问题仍然是它不起作用并且不显示我的观点。
这是我添加的代码:
parse: function(response) {
return response.Items;
}
更新
我终于回答了我的问题!万岁!不知何故,我忘了在我的 ItemListview 中渲染“()”。而且$("#ItemContainer") 似乎也不起作用,所以我去了$('#ItemContainer) 现在我正在显示我的模型的详细信息。
【问题讨论】:
标签: javascript backbone.js wcf-rest