我想一些數據加載到骨幹網收集從本地JSON文件,使用這種非常基本的代碼:從JSON文件將數據加載到Backbone集合中?
window.Student = Backbone.Model.extend({
});
window.Students = Backbone.Collection.extend({
model: Student,
});
window.AllStudents = new Students();
AllStudents.fetch({ url: "/init.json"});
console.log('AllStudents', AllStudents);
在控制檯中陳述,AllStudents
是空的。但是init.json
肯定會被加載。它看起來像這樣:
[
{ text: "Amy", grade: 5 },
{ text: "Angeline", grade: 26 },
{ text: "Anna", grade: 55 }
]
我在做什麼錯?
更新:我也嘗試添加一個reset
聽者.fetch()
撥打以上,但是這不點火之一:
AllStudents.bind("reset", function() {
alert('hello world');
});
AllStudents.fetch({ url: "/init.json"});
中不會顯示警報。
更新2:嘗試這個腳本(在這裏充分轉載):
$(function(){
window.Student = Backbone.Model.extend({
});
window.Students = Backbone.Collection.extend({
model: Student,
});
window.AllStudents = new Students();
AllStudents.url = "/init.json";
AllStudents.bind('reset', function() {
console.log('hello world');
});
AllStudents.fetch();
AllStudents.fetch({ url: "/init.json", success: function() {
console.log(AllStudents);
}});
AllStudents.fetch({ url: "/init.json" }).complete(function() {
console.log(AllStudents);
});
});
只有一個控制檯聲明甚至出現在第三fetch()
通話,這是一個空的對象。
我現在絕對很困惑。我究竟做錯了什麼?
JSON文件被作爲應用程序/ json提供,所以它與此無關。
這是一個很好的問題。你應該使用http://jsonlint.com/來驗證你的JSON – 2014-07-03 20:59:51