2014-03-19 27 views
0

在我的骨幹模型我試圖斷火到服務器的請求,從一些JSON填充集合添加請求的數據收集:在我account.js模型使用backbone.marionette

{ 
"accountType":["Full","Trial"] 
} 

我有:

accountManager.module("Entities", function(Entities, accountManager,Backbone, Marionette, $, _){ 

Entities.account = Backbone.Model.extend({}); 

Entities.accountCollection = Backbone.Collection.extend({ 
model: Entities.account, 
comparator: "account", 
url:"webapp/jsonData" 
}); 


var API = { 
getaccountEntities: function(){ 
var accounts = new Entities.accountCollection(); 
accounts.fetch(); 
if(accounts.length === 0){ 
console.log("No profiles loaded"); 
} 
return accounts; 
}}; 

accountManager.reqres.setHandler("account:entities", function(){ 
    return API.getaccountEntities(); 
}); 
}); 

Controller.js

accountManager.module("accountsApp.List", function(List, accountManager,Backbone, Marionette, $, _){ 

List.Controller = { 
    listaccounts: function(){ 
     var accounts = accountManager.request("account:entities"); 
     var accountsListView = new List.accounts({ 
     collection: accounts}); 
     accountManager.mainRegion.show(accountsListView); 
     } 
    }; 
}); 

app.js

var profileManager = new Marionette.Application(); 

profileManager.addRegions({ 
mainRegion: "#main-region", 
}); 

profileManager.on("initialize:after", function(){ 
profileManager.profilesApp.List.Controller.listprofiles(); 
}); 

如何用返回的數據填充集合,然後將其顯示在視圖中?

以下是我擁有的一本書中的教程,但似乎無法使其工作。

控制檯日誌錯誤(所以我知道這是沒有得到的數據),我也得到一個錯誤:

「未捕獲的ReferenceError:帳戶沒有定義」

有什麼建議?

+0

你的API返回什麼?它是否返回一個集合?從上面的問題來看,它看起來像是試圖將模型提取到集合中。 –

+0

嗨,感謝您的回覆。我已經更新了我的問題,我正在通過持久數據部分進行跟蹤,它在我返回靜態數據時起作用,但是當我試圖從正在運行的問題的服務器填充集合時。 – Hagbard

+0

我已經添加了我的app.js,account.js和控制器中的代碼。 – Hagbard

回答

1

您試圖通過使用不返回集合的API enpoint來初始化集合。 「Web應用程序/ jsonData」需要TOR E打開類似

[ 
    { id: 1, name: "account 1", type: "Full"}, 
    { id: 2, name: "account 2", type: "Trial"}, 
    { id: 3, name: "account 3", type: "Full"} 
] 

請注意,它的對象,每個對象將成爲集合中的模型的數組。

尤其是followign JSON(根據您的問題)不能創建一個集合:

{ 
    "accountType":["Full","Trial"] 
} 

相反,如果你想帳戶類型的集合,你的JSON必須像

[ 
    {name: "Full"}, 
    {name: "Trial"} 
] 

您的集合中還存在一個小錯誤:comparator值需要是對象屬性。換句話說,只有在每個賬戶模型都有「賬戶」屬性的情況下才能使用。

+0

哦,好吧,我明白了,我有一個遊戲集合,我將在這個過程中使用,我將不得不工作的其他東西,以獲得帳戶類型和填充下拉,謝謝 – Hagbard

相關問題