以下是我目前的进展:
var Item = Backbone.Model.extend({
defaults: {
id: 0,
pid: 0,
t: null,
c: null
},
idAttribute: 'RootNode_', // what should this be ???
url: 'page.php'
});
var ItemList = Backbone.Collection.extend({
model: Item,
url: 'page.php',
parse: function(data) {
alert(JSON.stringify(data)); // returns a list of json objects, but does nothing with them ???
}
});
var ItemView = Backbone.View.extend({
initialize: function() {
this.list = new ItemList();
this.list.bind('all', this.render, this);
this.list.fetch();
},
render: function() {
// access this.list ???
}
});
var view = new ItemView();
当前(预期)的JSON响应:
{
"RootElem_0":{"Id":1,"Pid":1,"T":"Test","C":"Blue"},
"RootElem_1":{"Id":2,"Pid":1,"T":"Test","C":"Red"},
"RootElem_2":{"Id":3,"Pid":1,"T":"Test2","C":"Money"}
}
这段代码可以成功地调用
page.php
,后端根据$_SERVER['REQUEST_METHOD']
执行并返回所需的信息,但我不知道为什么集合没有被填充。在
ItemList
的parse
函数中,它正确地显示了所有输出,但它对此无动于衷。我在代码中留下了一些注释以获得更精确的问题,但主要问题是为什么集合不会填充已经接收到的数据?
ItemView
的initialize
函数中创建并获取了ItemList
的实例。至于视图中的模型,这并不是使其工作所必需的。 - Nahydrinthis.list.fetch()
这行代码时,ajax 调用完成后会调用parse
方法,且data
参数包含一个有效的 json 对象。但是,集合并未用它收到的响应填充。 - Nahydrin