JavaScript:Backbone.js 填充模型集合
这是我到目前为止所得到的:
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
函数中,它正确地向我显示了所有输出,但它对此没有任何作用。
我在代码中留下了一些注释,以解决一些更精确的问题,但主要问题是为什么集合不填充明显收到的数据?
Here is what I have so far:
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();
Current (expected) json response:
{
"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"}
}
This successfully polls page.php
and the backend acts on $_SERVER['REQUEST_METHOD']
and returns the required information, however I don't know why the collection is not filled.
In the parse
function of ItemList
it properly shows me all the output, but it does nothing with it.
I left some comments in the code for some more precise questions, but the main question is why doesn't the collection populate with the obviously received data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的
parse
方法修改为:要遵循约定,请将
ItemView
内的list
更改为model
。同样在render()
中:Modify your
parse
method to:To follow conventions, change
list
insideItemView
tomodel
. Also inrender()
:您应该在执行必要的解析后返回数据的解析方法。
解析的常见用例是,如果您要发送回以下形式的对象:
那么您将像这样使用解析:
Backbone 然后处理其余部分。
您以该字典格式发回数据是否有特殊原因?目前尚不清楚您打算如何将其映射到该系列的每个模型。关键是不重要吗?如果是这样,您应该传回值中的对象列表。(尽管请参阅底部的注释)。如果没有,并且您想将其附加到模型,则应将其移动到您用作值的对象并发回列表。
* 注意:实际上不要直接发回 JSON 列表。 GET 请求有一个漏洞,该漏洞依赖于列表本身是有效的 javascript,其中恶意站点可以使用 Array 对象并覆盖它以使用 API 的脚本标记,从而使用用户凭据来获取任何可用信息在那次通话中。相反,当想要发回列表时,您应该使用类似以下的内容:
然后您只需使用上面的 parse 方法来提取列表。
The parse method you're supposed to be returning the data after doing whatever necessary parsing is required for it.
The common use case for parse would be if you're sending back an object of a form like:
then you'd use parse like so:
Backbone then handles the rest.
Is there a particular reason why you're sending the data back in that dictionary format? It's not exactly clear how you intend to map that to each model of the collection. Is the key irrelevant? if so, you should be passing back a list of the objects in the values.(Although see note at bottom). If not, and you want to attach it to the models, it should be moved to the object you're using as a value and send back a list.
* Note: Don't actually send back a JSON list bare. There is an exploit for GET requests that relies on lists being valid javascript on their own, where a malicious site can use the Array object and override it to use a script tag to your API to use the users credentials to pull down whatever information is available in that call. Instead, when wanting to send back a list you should use something like this:
Then you just use the parse method above to extract the list.