嵌套对象的问题

发布于 2024-12-20 14:54:49 字数 727 浏览 0 评论 0原文

我正在尝试使用 Backbone 创建嵌套模型,但有几个问题: 我将按照主干文档中的示例进行操作:假设我有一个邮箱集合,每个邮箱可以有一个消息集合。我想在我的应用程序中查看并处理这些嵌套列表。首先 - 我按照主干文档中所述更改我的邮箱模型。:

var Mailbox = Backbone.Model.extend({
  initialize: function() {
    this.messages = new Messages;
    this.messages.url = '/mailbox/' + this.id + '/messages';
    this.messages.bind("reset", this.updateCounts);
  },
});

我的第一个问题,当我使用服务器调用填充模型中的数据时(我通过急切加载来完成 - 我传入邮箱和嵌套消息)我可以加载数据到每个邮箱的属性中,但是当在 javascript 调试器中查看它时,我看到对象属性中的嵌套数据数组,但没有看到数据加载到实际的嵌套集合中。我做错了什么吗?就像有两组不同的信息。

第二个问题 - 当我显示嵌套索引视图时 - 我想显示一个邮箱列表,每个邮箱都有一个嵌套的消息列表 - 我尝试使用带有主干的嵌套视图,只有我的嵌套视图重复外部视图中其他对象的视图- 在各处创建这种奇怪的重复视图分组。我不明白我在这里做错了什么。有人可以在这里指点一下吗?

是否有任何好的明确指南不仅可以创建模型,还可以创建主干中带有嵌套对象的模型、集合和视图?

I'm trying to create a nested model with Backbone and have a couple of issues:
I'll follow with the example in the backbone documentation: say I have a Mailbox Collection, and each Mailbox can have a collection of Messages. I want to see and work on these nested lists in my app. First - I alter my Mailbox model as noted in the backbone documentation.:

var Mailbox = Backbone.Model.extend({
  initialize: function() {
    this.messages = new Messages;
    this.messages.url = '/mailbox/' + this.id + '/messages';
    this.messages.bind("reset", this.updateCounts);
  },
});

My first question, when I populate data in the model with a server call (which I do with eager loading - I pass in the Mailboxes and nested Messages) I can load the data into attributes on each Mailbox, but when looking at it in a javascript debugger, I see the array of nested data in my object's attributes, but I don't see the data loaded into the actual nested collection. Am I doing something wrong? It's like there's two different sets of information.

Second question - when I show a nested index view - I want to show a list of Mailboxes each with a nested list of Messages - I try to use nested views with backbone, only my nested views repeat the views of other objects from the outer view - creating this strange grouping of duplicate views everywhere. I can't figure out what I'm doing wrong here. Might anyone have any pointers here?

Is there any good definitive guidance on creating not just models, but models, collections and views with nested objects in backbone?

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

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

发布评论

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

评论(1

孤千羽 2024-12-27 14:54:49

我在 Documentcloud 的 IRC 频道上找到了两个问题的答案。

对于第一个问题,这只是公开数据的练习。您可以创建一个全局变量(集合或路由器的)并在那里公开数据......这取决于用户。

对于第二个问题,我遇到了一个问题,我正在使用 Rails-backbone gem,当使用该 gem 的脚手架创建嵌套视图时,您最终会使用 jquery 在多个位置附加视图,因为 gem 不期望在文档中拥有多个 tbody 标签。

I found out answers to both my questions on the IRC channel for documentcloud..

For the first question, it's simply an exercise in exposing that data. You can either create a global variable (of a collection or router) and expose the data there... That's just up to the user.

For the second question, I had an issue where I was using the rails-backbone gem, and when using the scaffolding for that gem to create nested views, you wind up using jquery to append views in multiple places, because the gem is not expecting to have multiple tbody tags within a document.

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