我從RESTful服務這一結果存取模式,我有: 不能在Backbone.js的
注:響應是JSON格式,它是由鉻,顯示它像插件那。
如果你看看圖像二[上面這個]模型屬性是項目然後每個項目都在項目下。我應該怎麼做才能訪問商品?
我的問題是我無法從這個結果中訪問或檢索每個項目的數據。但我絕不能從服務器端改變任何東西。我正在使用這個代碼的骨幹。
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();
UPDATE
我能夠糾正我的嵌套JSON對象的問題。現在,模型屬性或我的集合中將填充單個項目。但問題仍然存在,它不起作用,並不顯示我的觀點。
這是我添加的代碼:
parse: function(response) {
return response.Items;
}
UPDATE
我最後回答我的問題! horray!不知何故,我忘了在我的ItemListview中渲染「()」。也$("#ItemContainer")
似乎並不工作,所以我現在$('#ItemContainer)
現在我顯示我的模型的細節。
編號響應採用JSON格式。不知何故,我在Chrome上添加了一個插件,使其成爲您顯示的圖像,但我確信響應是以JSON形式顯示的。 – jongbanaag 2012-03-23 03:19:35
您不需要重寫同步。您只需傳遞選項即可獲取並保存以處理XML。例如與獲取,你會做這樣的事情: this.model.fetch({ 的contentType: 「應用/ JSON」, 數據類型: 「XML」, 過程數據:假 });奇蹟般有效。 – 2012-04-13 01:41:20