JavaScript:Backbone.js 填充模型集合

发布于 2025-01-04 07:25:00 字数 1246 浏览 1 评论 0原文

这是我到目前为止所得到的:

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'] 并返回所需信息,但我不知道为什么集合没有填满。

ItemListparse 函数中,它正确地向我显示了所有输出,但它对此没有任何作用。

我在代码中留下了一些注释,以解决一些更精确的问题,但主要问题是为什么集合不填充明显收到的数据

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼睛会笑 2025-01-11 07:25:00

将您的 parse 方法修改为:

parse: function(response){
   var parsed = [];
   for(var key in response){
      parsed.push(response[key]);
   }
   return parsed;
}

要遵循约定,请将 ItemView 内的 list 更改为 model。同样在 render() 中:

render: function() {
    var template = _.template("<div>some template</div>");
    this.model.each(function(item){ 
        this.$el.append(template(item.toJSON()));
    }, this);
    return this;
}

Modify your parse method to:

parse: function(response){
   var parsed = [];
   for(var key in response){
      parsed.push(response[key]);
   }
   return parsed;
}

To follow conventions, change list inside ItemView to model. Also in render():

render: function() {
    var template = _.template("<div>some template</div>");
    this.model.each(function(item){ 
        this.$el.append(template(item.toJSON()));
    }, this);
    return this;
}
歌枕肩 2025-01-11 07:25:00

您应该在执行必要的解析后返回数据的解析方法。

解析的常见用例是,如果您要发送回以下形式的对象:

{ "id" : "NaN", "tasks": [ *all your models in a list here *] }

那么您将像这样使用解析:

parse: function (data) {
    return data.tasks
}

Backbone 然后处理其余部分。

您以该字典格式发回数据是否有特殊原因?目前尚不清楚您打算如何将其映射到该系列的每个模型。关键是不重要吗?如果是这样,您应该传回值中的对象列表。(尽管请参阅底部的注释)。如果没有,并且您想将其附加到模型,则应将其移动到您用作值的对象并发回列表。

* 注意:实际上不要直接发回 JSON 列表。 GET 请求有一个漏洞,该漏洞依赖于列表本身是有效的 javascript,其中恶意站点可以使用 Array 对象并覆盖它以使用 API 的脚本标记,从而使用用户凭据来获取任何可用信息在那次通话中。相反,当想要发回列表时,您应该使用类似以下的内容:

{ result: [*list here*] }

然后您只需使用上面的 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:

{ "id" : "NaN", "tasks": [ *all your models in a list here *] }

then you'd use parse like so:

parse: function (data) {
    return data.tasks
}

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:

{ result: [*list here*] }

Then you just use the parse method above to extract the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文