Skip to content Skip to sidebar Skip to footer

$.when Not Waiting For Ajax Request To Finish

I want to first render a view with Backbone.js that displays an Article pulled from the server. I then want to mark this as 'seen' and return the count of unseen messages to the ro

Solution 1:

You need to return a jQuery Deferred object for $.when to work properly:

return article_view.save(viewDetails, 
    {
    success: function(data) {
            var count = data.get('count');   
            console.log('in saveView() success and count is ');
            console.log(count); 
            return count;     
        },
        error:   function(model, xhr, options){
           console.log(xhr.responseText);
        },
    });

Backbone's save() method returns a jqXHR object which behaves the same way as a Deferred object in this case. Simply chain the return call as above. This should get $.when() to wait for the request to finish.

Post a Comment for "$.when Not Waiting For Ajax Request To Finish"