Trunk relational: the association key will not work if it does not match the foreign key

I am trying to get a backbone-relational plugin that works with the association between tasks and messages. (The task has many messages).

Information is pulled from the standard rails / activerecord site, which has a task_id field as a foreign key.

The problem is that the basic relational relationship does not fill the "message" field with any messages in the task model, unless I set the key as "task_id" in the opposite way ... but this means that when accessing the task from the Message model, the field task_id is populated with the actual task object, not the integer task_id that is overwritten.

I assume that there is an easy way to specify task_id as a foreign key with which you can define the parent task, but it has an object that is a key that is placed in another field (for example, the "task" in the message object). but I can’t figure out how to do this. 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 } }] 
+6
source share
2 answers

You may be able to use keySource or keyDestination to solve your specific problem.

Example

In the following example, suppose we get 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 the foreign Monster_Id foreign key in the Loot_Item table. Suppose also that our REST service does not have the slightest detail for us, since this seems to be quite close to the situation in your question.

keySource

Now, let the set set “keySource” to my foreign key (“Monster_Id”) and “key” to the attribute name, where I want the actual data to go (say, “Monster”). If you go into the debugger, you will see in the attribute object that there is actually a field called "Monster" and that it points to the data of the monster model. Hey cool!

includeInJSON

However, if you are to this puppy, guess what? He put all the monster data in Monster_Id, just like you didn’t want! Gah! We can fix this by setting "includeInJSON" to "Monster_Id". Now, when it is converted to JSON format, it puts the correct identifier back into the Monster_Id field when it serializes data in JSON format to send to the server.

Is the problem resolved? Er, well, actually, not necessarily ...

CAVEAT . It all sounds super-useful, but there is one pretty obvious problem I found with this scenario. If you use a template engine (for example, one in Underscore.js) that requires you to convert your model to JSON before passing it to a template, exclamations - you do not have access to relational data. Alas, the JSON that we want for our posts is not necessarily the same JSON that we want to feed into our templates.

+3
source

If you want the "task_id" in the JSON message to be an identifier and not the full JSON for the task, then set "includeInJSON" as the property "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: "task_id" } }] 

The "true" value for includeInJSON says to use full JSON for the corresponding model.

Edit: After re-reading my question, I'm not sure if my answer relates to your problem.

My initial answer is to send a message to the server where you want JSON to be something like:

 { "message_title": "My Title", "message_body": "Blah blah blah...", "task_id": 12345 } 

I'm not sure what exactly you want, but the way Backbone Relational should work is that the Task message collection will be a collection of complete models, so you can iterate over them and pass them to the views for rendering, etc.

If you want to output one of the message identifiers to a template or something else, you should take the message model "id":

 myTask.get('messages').first().id -> returns the first message id 
0
source

Source: https://habr.com/ru/post/904929/


All Articles