Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Model Implied Relationships & Serialization = Too Much Recursion?

$
0
0
I've got some CanJS models roughly arranged like so:

  1. User = can.Model({
  2.   attributes: {
  3.     activities: 'Activity.models',
  4.   }
  5. });
  6. Activity = can.Model({
  7.   attributes: {
  8.     user: 'User.model',
  9.   }
  10. });

My API sends back user IDs with activities and it looks like CanJS wires up the relationship automatically.  Example:

foo = User.new({id: "123"})

and then if
  1. bar = Activity.findOne("456")
returns an activity with { ... user: {id: "123"} } CanJS will ensure that:
  1. bar.user === foo

That's neat.  But when I do bar.serialize() CanJS tries to serialize the activity, which tries to serialize the user, which descends into the activity again and on and on...

What's the right way to handle these circular relationships when serializing?

Viewing all articles
Browse latest Browse all 3491

Trending Articles