How do I call fetch method from within a render method in Backbone.js?
-
Hi I am trying to call fetch method to get a collection of models from inside a render method but the before the fetch method gets called other lines of the render method are executed and finally the fetch method is called. Can anyone help me to pause the lines following the fetch method till all the models are fetched and then complete the render method. Thanks
-
Answer:
You could override the fetch method and trigger callbacks on success and error of your fetch method. Check the code below... Model var TestModel = Backbone.Model.extend({ fetch: function () { var self = this; var fetchOptions = { success: function (model, data) { self.trigger('TestView:ResponseReceivedSuccess', data); }, error: function (model, error) { self.trigger('TestView:ResponseReceivedError', error); }, type: 'GET', contentType: 'application/json', async: false }; self.trigger('TestView:RequestSent'); Backbone.Model.prototype.fetch.call(self, fetchOptions); } }); View var TestView = Backbone.View.extend({ initialize: function () { this._bindModelChangeEvents(); this.model.fetch(); }, _bindModelChangeEvents: function () { var self = this; this.model.on('TestView:ResponseReceivedError', function () { // error while fetch }); this.model.on('TestView:ResponseReceivedSuccess', function (data) { // do something with your data here }); } });
Yasser R Shaikh at Quora Visit the source
Other answers
The problem is the fetch method is a asynchronous one. So your remaining piece of code inside render function gets executed and asynchronously the fetch method gets executed. You can have a return soon after the fetch method is called and inside fetch onSuccess execute the render function.
Rohith Menon
Related Q & A:
- How do I change HTML content to be a JS variable?Best solution by ehow.com
- How do I add to an array from a static method?Best solution by stackoverflow.com
- How can I overload equal method to make different objects have same hashcode value in unordered_multimap?Best solution by Stack Overflow
- How can I make a phone call to a cruise ship?Best solution by traveltips.usatoday.com
- How do I call a cell phone in Australia?Best solution by wiki.answers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.