by kuschku · 11y ago · 0 comments
Or you send it like
{
"users": [
{
"id": "de305d54-75b4-431b-adb2-eb6b9e546014",
"name": "Max Mustermann",
"image": "https://news.ycombinator.com/y18.gif"
},
...
],
"comments": [
{
"user": "de305d54-75b4-431b-adb2-eb6b9e546014",
"time": "2015-10-15T18:51:04.782Z",
"text": "Or you can do this"
},
...
]
}
Which is a standard way to do it that works here, too. Because you can have references in JSON, you just have to do them yourself.But it still takes more power for your server to go through gzip every time. And it will take more RAM on the client to store those objects.
But not exactly pure JSON. Client-side, you need to attach methods (or getters) that fetch the user object to the comment. I suppose you could just attach get(reference) that takes this[reference + '_id'] and looks it up inside the `result[reference]`. m:n relations will be harder though.
Otherwise you can't e.g. simply pass each comment to e.g.a react "Comment" component that renders it. You would also have to pass the user list (dictionary) to it.
response.comments.forEach(comment => comment.author = response.user[comment.author]);
Preferably, even, you could do it during rendering so it can be garbage collected afterwards.