主干关系:除非与外键相同,否则关联键将不起作用
我正在尝试让 backbone-relational 插件处理任务和消息之间的关联。 (一个任务有很多消息)。
该信息是从标准的rails/activerecord 站点中提取的,该站点有一个task_id 字段作为外键。
问题是,骨干关系不会用任务模型上的任何消息填充“消息”字段,除非我将密钥设置为反向关系中的“task_id”......但这意味着,当从消息模型,task_id 字段填充实际任务对象,而不是被覆盖的“task_id”整数。
我猜测有一种简单的方法可以指定 task_id 作为外键来确定父任务,但将键代表的对象放置在不同的字段中(例如消息对象上的“任务”)...但是我不知道怎么办。任何想法表示赞赏。代码如下
class Backbonescaffolddemo.Models.Task extends Backbone.RelationalModel
paramRoot: 'task'
relations: [{
type: Backbone.HasMany,
key: "messages",
relatedModel: "Backbonescaffolddemo.Models.Message",
collectionType: "Backbonescaffolddemo.Collections.MessagesCollection",
includeInJSON: true
reverseRelation: {
key: "task_id"
includeInJSON: true
}
}]
I'm trying to get the backbone-relational plugin working with an association between tasks and messages. (A task has many messages).
The information is pulled from a standard rails/activerecord site, which has a task_id field as the foreign key.
The problem is, backbone-relational won't populate the 'messages' field with any messages on teh Task model unless I set the key as "task_id" in the reverse relation...but that means that, when accessing the task from the Message model, the task_id field is populated with the actual task object, not the 'task_id' integer, which is overwritten.
I'm guessing there's a simple way to specify task_id as the foreign key with which to determine the parent task, yet have the object that key represents placed in a different field (eg 'task' on the messages object)...but I can't figure out how. Any ideas appreciated. Code below
class Backbonescaffolddemo.Models.Task extends Backbone.RelationalModel
paramRoot: 'task'
relations: [{
type: Backbone.HasMany,
key: "messages",
relatedModel: "Backbonescaffolddemo.Models.Message",
collectionType: "Backbonescaffolddemo.Collections.MessagesCollection",
includeInJSON: true
reverseRelation: {
key: "task_id"
includeInJSON: true
}
}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也许可以使用 keySource 或 keyDestination 来解决您的特定问题。
示例
在下面的示例中,假设我们从老式关系数据库获取数据,其中 Monster 和 Loot_Item 之间存在一对多关系。这种关系由 Loot_Item 表中的 Monster_Id 外键表示。我们还假设我们的 REST 服务不会为我们做任何花哨的数据嵌套,因为这似乎与您问题中的情况非常匹配。
keySource
现在,让我们将“keySource”设置为我的外键(“Monster_Id”),并将“key”设置为我想要实际数据所在的属性的名称(例如“Monster”)。如果您中断调试器,您将在属性对象中看到实际上有一个名为“Monster”的字段,并且它确实指向怪物模型数据。嘿,酷!
includeInJSON
然而,如果你 toJSON 那只小狗,你猜怎么着?它已经把所有的怪物数据都放在Monster_Id里了,就像你不想要的一样!嘎啊!我们可以通过将“includeInJSON”设置为“Monster_Id”来解决这个问题。现在,当它转换为 JSON 时,它将正确的 ID 放回到 Monster_Id 字段中,同时将数据序列化为 JSON,以发送到服务器。
问题解决了吗?呃,好吧,实际上,不一定...
警告:这一切听起来非常有用,但我在这个场景中发现了一个相当明显的问题。如果您使用的模板引擎(例如 Underscore.js 中的模板引擎)要求您将模型转换为 JSON,然后再将其传递到模板中,那么您将无权访问关系数据。遗憾的是,我们想要的消息 JSON 不一定与我们想要输入到模板中的 JSON 相同。
You may be able to use keySource or keyDestination to address your particular problem.
Example
In the following example, suppose we are getting data from an old-school relational database, where there is a one-to-many relationship between Monster and Loot_Item. This relationship is expressed by a Monster_Id foreign key in the Loot_Item table. Let us also suppose that our REST service doesn't do any fancy-pants data nesting for us, since that seems to match the situation in your question fairly closely.
keySource
Now, let's set set "keySource" to my foreign key ("Monster_Id") and "key" to the name of the attribute where I want the actual data to go (say, "Monster"). If you break in the debugger, you will see in the attributes object that there is, in fact, a field called "Monster", and that it does point to the monster model data. Hey, cool!
includeInJSON
However, if you toJSON that puppy, guess what? It has put all the monster data in Monster_Id, just like you didn't want! GAH! We can fix that by setting "includeInJSON" to "Monster_Id". Now, when it is converted to JSON, it puts the proper ID back into the Monster_Id field, when it is serializing your data to JSON, to send up to the server.
Problem solved? Er, well, actually, not necessarily...
CAVEAT: This all sounds super-useful, but there's one fairly glaring problem that I have found with this scenario. If you are using a templating engine (such as the one in Underscore.js) that requires you to convert your model to JSON, before passing it into the template, whoops -- you don't have access to your relational data. Alas, the JSON that we want for our messages is not necessarily the same JSON that we want to feed into our templates.
如果您希望消息 JSON 中的“task_id”成为 id,而不是任务的完整 JSON,则将“includeInJSON”设置为任务的 ID 属性(“task_id”)
includeInJSON 的“true”值表示为使用相关模型的完整 JSON。
编辑:重新阅读您的问题后,我不确定我的答案是否与您的问题相关。
我最初的答案是向服务器发送一条消息,您希望 JSON 类似于:
我不确定您到底想要发生什么,但是 Backbone Relational 应该工作的方式是任务的消息集合将是完整模型的集合,因此您可以迭代它们并将它们传递给视图进行渲染等。
如果您想在模板或其他内容中输出消息的 id 之一,那么您可以采用消息模型 “ID”:
If you want the "task_id" in the message JSON to be the id, not the full JSON for the task, then set the "includeInJSON" to be the Task's ID property ("task_id")
The "true" value for includeInJSON says to use the full JSON for the related model.
Edit: After re-reading your question, I'm not sure my answer relates to your issue.
My original answer is for posting a message back to the server where you want the JSON to be something like:
I'm not sure what exactly you're looking to happen, but the way that Backbone Relational is supposed to work is that the Task's collection of messages will be a collection of the full models, so you can iterate over them and pass them to views for rendering, etc.
If you want to output one of the Message's id's in a template or something, then you'd take the Message model's "id":