General question if I am calling a webservice to return data related to one of my Backbone models. Would it be better to call the webservice from within the model itself or should I pass in the results of the WS to the model on initialization.
I’m leaning towards incorporating the WS call within my model but then obviously may run into latency issues.
So where is the best place to handle this data retrieval?
If the page that is rendering your HTML already knows the data that will go into your model, I definitely prefer rendering the data right into the model constructor. Something like this (assuming a Rails view, but that is just for the purpose of illustration):
Let’s assume your controller has rendered some JSON data as
@modelData.Doing it this way allows you to have your data immediately and not require a second call back to the service. I have used this approach several times with a lot of success.
Edit
To expand on this, this results in fewer calls to the server because the back end is rendering the data in the HTML or JS that gets returned to the client. The result (after view rendering) of the above code might be something like this:
If you to it the other way, however, you make two calls to the server:
The first call to the server is the one returning your code. The second happens when you call fetch.
If you are asking something else, then I apologize. You might clarify what you mean by “where is the best place to handle these calls”.