2014-10-11 186 views
0

林與骨幹玩弄和正在經歷的東西我不太明白:骨幹:從收集得到的模型

1)我已經創造了一些模擬數據的「數據/ applications.js」

[ 
    { 
    "id": "1", 
    "title": "Title #1", 
    "image": "some/path1" 
    }, 
    { 
    "id": "2", 
    "title": "Title #2", 
    "image": "some/path2" 
    }, 
    { 
    "id": "3", 
    "title": "Title #3", 
    "image": "some/path4" 
    }, 
    { 
    "id": "4", 
    "title": "Title #4", 
    "image": "some/path4" 
    } 
] 

2)我試着收集

ApplicationModel = Backbone.Model.extend({}); 

ApplicationCollection = Backbone.Collection.extend({ 
    url: "data/applications.json", 
    model: ApplicationModel 
}); 

var applications = new ApplicationCollection(); 
applications.fetch(); 
var application = applications.get(applicationId); // I get this from route 

console.log(applicationId); // returns 2 
console.log(applications); // returns the collection with 4 models 
console.log(application); // returns undefined 

我有一種感覺,我在想念的東西在檢索單個「應用程序」?

回答

1

當您使用Backbone獲取數據時,它是異步完成的,這意味着腳本在獲取時會繼續執行。要檢索您的應用程序,您必須等待提取結束,如下所示:

applications.fetch({success: function() { 
     console.log(applications.get(applicationId)); 
}}); 
+0

當然。不能相信我錯過了這一點。謝謝 :) – tolborg 2014-10-11 13:37:45