Bootstrapping data with Backbone using one query as opposed to multiple queries
Asked Answered
C

2

0

Please correct me if I am wrong, but is one complex query less expensive than multiple small queries -> Question? Seems the question listed has opposing views. I was always taught that the most expensive operation is the opening and closing of the connection to the db. If that is the case, what is the most efficient way to bootstrap your data on application startup?

For simplicities sake, lets say my application requires a collection of latest posts, latest categories, the logged in user, and latest tags. Now I could fetch all this data in one query, or I could fetch this data individually. Which one would you recommend? If I went with a single query, how do you usually do this in Backbone, and how would you do this over a Restful API?

Thanks so much

Tyrone

Clique answered 7/6, 2012 at 7:13 Comment(1)
Related: #9781602Covey
E
0

backbone-relational (which you mention) provides a solution inside backbone without using jQuery.getJSON as suggested in the related question. Simply return everything in a response to the root query and define relations matching the JSON structure.

For example, I've just written something which returns an array of relevant profile objects in the root of the response, and then uses profile_ids elsewhere. Just be sure to define both the top-level relation and the references (using keySource and keyDestination).

Extracanonical answered 19/6, 2012 at 6:41 Comment(2)
I must play with Backbone relational. So if I define the relationships in the model and return the json string including the relationships will they be mapped to their respective models?Clique
Yes. It wasn't obvious from the documentation, but I've verified the behaviour through testing.Extracanonical
P
0

Multiple requests are executing asynchronously so total time is not a sum of all request times. As with any optimization you should check potential gain, probably you don't have to bother than with that at all.

There are quite a few possible approaches in the related question (I like Backbone because of such flexibility).

There is another one which you can achieve the goal in zero additional requests with. You can embed all needed data into page's script tags on the server side when generating the page.

Perambulate answered 7/6, 2012 at 10:9 Comment(0)
E
0

backbone-relational (which you mention) provides a solution inside backbone without using jQuery.getJSON as suggested in the related question. Simply return everything in a response to the root query and define relations matching the JSON structure.

For example, I've just written something which returns an array of relevant profile objects in the root of the response, and then uses profile_ids elsewhere. Just be sure to define both the top-level relation and the references (using keySource and keyDestination).

Extracanonical answered 19/6, 2012 at 6:41 Comment(2)
I must play with Backbone relational. So if I define the relationships in the model and return the json string including the relationships will they be mapped to their respective models?Clique
Yes. It wasn't obvious from the documentation, but I've verified the behaviour through testing.Extracanonical

© 2022 - 2024 — McMap. All rights reserved.