I have a question about handling my models. I get all confused. When I load the page I get a JSON string from rails containing “events” these events have in turn one user, multiple participants, multiple payments and multiple comments, these comments, have in turn one user, and the payments have multiple users and a single user. The comments and payments also have an event pointing back att the parent.
Events
User
Participants(users)
Payments
User
Users
Event
Comments
User
Event
Okey, so the question is: Should I load everything as a tree, with full attributes everywhere:
"events": {
"id": "event_1",
"user": {
"id": "user_1", "name":"name"
}, "participants": [
{"id": "user_1", "name":"name"},
{"id": "user_2", "name":"name"}
], "payments": [{
"id":"payment_1",
"user": {
"id": "user_1", "name":"name"
},"users": [
{"id": "user_1", "name":"name"},
{"id": "user_2", "name":"name"}
], "event": {root event object}
}], "comments": [{
"id": "comment_1",
"user": {
"id": "user_1", "name":"name"
}, "event": {root event object}
}]
}
}
And then have the events model, to create new comments, payments and users, and assign it to it’s own event, or is it a better idea to load every event, user payment and comment into separate variables, and then use the variable to get the models.
It is quite hard to explain, so feel free to ask if I need to clarify something.
Conclusion: Should I let the event model handle the creation of all the nested objects, or is there some better way to handle this, and have access to the models more globaly?
Architecture is subjective, but here is how I would go about it –
3 base models
3 Collections
One Event model
If you initialize your event object with the JSON in the above example, it should just work. In the future if you want to separate out your users to a different API, this code should support that too. If you do want to make the component objects available globally later on, you can just get it from the user object, i.e., window.User = myEvent.get(“user”)
Hope this helps 🙂