I would like to use two models:
User:
- firstname : stringField
- lastname : stringField
Event:
- description : stringField
- users : list of referenceField(User)
When queried for an event, my REST API can output two data structure:
API JSON Output:
{
description: "MyDescription",
users: [
{ $id: user_1_id },
{ $id: user_2_id }
]
}
or
{
description: "MyDescription",
users: [
{ $id: user_1_id, firstname : "user_1_firstname", lastname: "user_2_lastname" },
{ $id: user_2_id, firstname : "user_2_firstname", lastname: "user_2_lastname" }
]
}
Which one of those two structure is better for nesting models into backbone.js?
Both approaches will work, with the first you would use the id from the list to get the complete model from your users collection, and with the second you are basically including a complete copy in the event’s user list.
The first JSON output will probably be better choice if you will need to reference the User models from more then one place. Being that you indicated that the list of users within the Event model is a reference list I would say this is probably the approach you want to take.
On the other hand if you are only going to have one Event model or if all your event models have separate users then you may as well use the second JSON output and store everything in the Event Model (especially since your user models only have a couple of attributes, but perhaps that’s just for illustration).