Search code examples
jsdata

Is there a way to model relationships only by url?


Let's imagine we get this JSON from the server:

{
 id: 1,
 name: "John",
 comments: [{
  id: 1,
  comment: "some comment"
 }]
}

Now I'd like this answer to be parsed like this:

UserModel
   ---- has (0 to n) ---> CommentModel

I know that if I'd add userId to the comment and specify it as foreignKey in the hasMany relationship that it would work.

But I'm wondering, if it can be done without having a foreign key on the comment. I mean the relationship is quite obvious by the data itself with comments being an array on the user and its url being http://somehost/user/1/comment/1.


Solution

  • You don't need the foreignKey on the comment in order for the nested comments to be auto-injected into the right part of the store, but you should still have the foreignKey defined in the relation definition so things like loadRelations will still work.