I have a multiple instances of a view which share single instance of model among themselves.
During rendering of view, I want to call a function inside model which makes server call to fetch some data only once.
As these views are instances of same view, they all are triggering the function inside model. Hence making multiple server call.
Any idea how i can trigger this function inside model only once.
Assuming you are calling
fetch
on the Model. This call will return the request (actually a jqXHR object). So a pattern which can be very useful is:This will save the request when fetch is called and avoid any additional calls while the current request is in-progress or if it has completed successfully.
Because the jqXHR object is a Deferred Promise object, anytime
fetchOnce
is called, callbacks can always be added (like deferred.done):