从backbone.js集合中获取JSON对象

发布于 2024-12-28 23:31:16 字数 851 浏览 0 评论 0原文

我从 RESTful API 获取 JSON 提要。我从服务器获得的 JSON 是一个包含 2 个不同对象数组的对象。我将该 JSON 分解为几个不同的 Backbone 集合,效果很好。我的主干对象看起来像这样:

Document = Backbone.Model.extend({
    defaults:{
        did: '',
        fields: []
    }
});

Documents = Backbone.Collection.extend({
    model: Document
});

对象本身很好,但我尝试使用 Handlebars.js 进行模板化,并发现必须采用对象,而不是数组。当我获取 Documents 对象并执行 .toJSON() 时,它似乎返回一个数组,而不是一个对象。所以我从服务器获得的原始 JSON 看起来像:

{
"Docs":[
    {"Fields":[{"ID":59,"VAL":""},{"ID":157,"VAL":""}],"Id":4143},
    {"Fields":[{"ID":59,"VAL":""},{"ID":69,"VAL":""}],"Id":4142}
]
}

但是一旦它被推入主干集合然后拉回以进行车把交接,它看起来像这样:

[
    {"Fields":[{"ID":59,"VAL":""},{"ID":157,"VAL":""}],"Id":4143},
    {"Fields":[{"ID":59,"VAL":""},{"ID":69,"VAL":""}],"Id":4142}
]

我怎样才能让主干给我返回对象而不仅仅是一个包含对象内容的数组?

I am geting a JSON feed from a RESTful API. The JSON I get from the server is an object with 2 different arrays of objects. I am breaking that JSON into a couple different Backbone collections which works fine. My backbone objects looks like this:

Document = Backbone.Model.extend({
    defaults:{
        did: '',
        fields: []
    }
});

Documents = Backbone.Collection.extend({
    model: Document
});

The objects themselves are fine, but I am trying to use Handlebars.js for templating and have found the must take an object, not an array. When I take my Documents object and do a .toJSON(), it seems to return an Array, not an object. So the raw JSON i get from the server looks like:

{
"Docs":[
    {"Fields":[{"ID":59,"VAL":""},{"ID":157,"VAL":""}],"Id":4143},
    {"Fields":[{"ID":59,"VAL":""},{"ID":69,"VAL":""}],"Id":4142}
]
}

But once it gets pushed into the backbone collection then pulled back out for the handlebars handoff, it looks like this:

[
    {"Fields":[{"ID":59,"VAL":""},{"ID":157,"VAL":""}],"Id":4143},
    {"Fields":[{"ID":59,"VAL":""},{"ID":69,"VAL":""}],"Id":4142}
]

How can I get backbone to give me back the object rather than just an array with the contents of the object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

罪歌 2025-01-04 23:31:16

backbone 为您提供集合中的数组,因为这就是集合所保存的内容——模型数组。

我不确定我是否清楚您要做什么,但看起来您真正需要的只是让服务器接收一个如下所示的文档:

{
    "Docs": docCollection.toJSON()
}

如果是这种情况,我只需覆盖您的 Documents 类上的 toJSON 方法可以执行以下操作:

toJSON: function() {
    return {"Docs": Backbone.Collections.prototype.toJSON.apply(self, arguments)};
}

这应该几乎可以满足您的要求,至少按照我的理解。

backbone gives you an array from a collection because that is what the collection holds -- an array of models.

I'm not sure I'm clear on exactly what you're trying to do, but it looks like all you actually need is to have the server receive a document that looks like:

{
    "Docs": docCollection.toJSON()
}

If this is the case, I'd just override the toJSON method on your Documents class to do something like:

toJSON: function() {
    return {"Docs": Backbone.Collections.prototype.toJSON.apply(self, arguments)};
}

This should pretty much do what you say you want, at least as I understand it.

稚气少女 2025-01-04 23:31:16

不幸的是,Backbone 系列在车把上的表现不如其应有的效果。由于它返回一个普通的 JavaScript 对象,因此您必须这样引用它。

尝试:

{{#each []}}
  <tr>
    <td>{{ this.title }}</td>
    <td>{{ this.start }}</td>
    <td>Invited</td>
    <td>Confirmed</td>
    <td>Edit</td>
  </tr>
{{/each}}

Unfortunately Backbone collections don't play as nicely as they should with handlebars. Since it returns a plain JavaScript object you have to reference it as such.

Try:

{{#each []}}
  <tr>
    <td>{{ this.title }}</td>
    <td>{{ this.start }}</td>
    <td>Invited</td>
    <td>Confirmed</td>
    <td>Edit</td>
  </tr>
{{/each}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文