Currently, I am in the process of developing a website that involves numerous relational links between data. For instance, users have the ability to create bookings
, which will include a booker
and a bookee
, as well as an array of messages
that can be associated with a booking
.
An illustration in JSON format would look like this...
booking = {
id: 1,
location: 'POST CDE',
desc: "Awesome stackoverflow description."
booker: {
id: 1, fname: 'Lawrence', lname: 'Jones',
},
bookee: {
id: 2, fname: 'Stack', lname: 'Overflow',
},
messages: [
{ id: 1, mssg: 'For illustration only' }
]
}
My main question is, how should one structure this data in an Angular app? Furthermore, how would you retrieve it from the server?
From my perspective, there are a few approaches.
Retrieve all data at once from the server
In this scenario, I would depend on the server to serialize the nested data and directly utilize the provided JSON object. The drawbacks here are that I wouldn't know which users are involved when requesting a booking or similar objects, making caching impossible, resulting in pulling a large amount of data each time a request is made.
Retrieve booking with booker/bookee represented as user ids
Here, I would employ promises for my data models and have the server send back an object like...
booking = {
id: 1,
location: 'POST CDE',
desc: "Awesome stackoverflow description."
booker: 1, bookee: 2,
messages: [1]
}
This object would then be passed to a Booking
constructor, where I would resolve the relevant (booker
,bookee
, and message
) ids into data objects using their respective factories.
The downside here is that multiple ajax requests are made for a single booking request, although it allows for caching user/message information.
To summarize, is it best practice to rely on a single ajax request to gather all nested information at once, or use various requests to fill in additional details after receiving the initial response.
It's worth mentioning that I'm utilizing Rails 4 (maybe Rails would better suit a single request approach?)