2012-03-23 67 views
0

我從RESTful服務這一結果存取模式,我有: enter image description here不能在Backbone.js的

enter image description here

注:響應是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對象的問題。現在,模型屬性或我的集合中將填充單個項目。但問題仍然存在,它不起作用,並不顯示我的觀點。

enter image description here

這是我添加的代碼:

parse: function(response) { 
return response.Items; 
} 

UPDATE

我最後回答我的問題! horray!不知何故,我忘了在我的ItemListview中渲染「()」。也$("#ItemContainer")似乎並不工作,所以我現在$('#ItemContainer)現在我顯示我的模型的細節。

回答

0

我很確定Backbone默認情況下對所有請求都使用JSON,並且不知道如何處理XML,您可能需要重寫集合的同步方法才能使用XML。

以下應解決您的問題有所幫助:http://newcome.wordpress.com/2011/02/20/consuming-xml-web-services-in-backbone-js-using-jath/

他們在該模型JSON其主幹可以本地使用轉換同步解析操作使用第三方庫的XML解析器。

+0

編號響應採用JSON格式。不知何故,我在Chrome上添加了一個插件,使其成爲您顯示的圖像,但我確信響應是以JSON形式顯示的。 – jongbanaag 2012-03-23 03:19:35

+0

您不需要重寫同步。您只需傳遞選項即可獲取並保存以處理XML。例如與獲取,你會做這樣的事情: this.model.fetch({ 的contentType: 「應用/ JSON」, 數據類型: 「XML」, 過程數據:假 });奇蹟般有效。 – 2012-04-13 01:41:20

0

確保響應返回爲JSON。 Backbone默認使用JSON數據。

+0

它是JSON格式。只是從鉻的插件,使得它是這樣的。 :) – jongbanaag 2012-03-23 03:21:27