$。当不等待Ajax请求完成时 [英] $.when not waiting for Ajax request to finish

查看:144
本文介绍了$。当不等待Ajax请求完成时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想首先使用Backbone.js呈现一个视图,该视图显示从服务器中提取的文章。然后,我想将此标记为已查看,并将未看到的消息计数返回给路由器,因为它需要可供其他视图使用。

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 router as it needs to be made available to other views.

所以在我的路由器中,我有:

So in my Router, I have:

    getArticle: function (id) {

        require(["app/models/article", "app/views/Article"], function (models, Article) {

                var article = new models.Article({id: id});

                article.fetch({
                    success: function (data) {

                        var articleView = new Article({model: data, message_count:that.message_count});

                        slider.slidePage(articleView.$el);

                        $.when(articleView.saveView()).done(function(data){
                            console.log('in when and data is ');
                            console.log(data);
                        });

                    },
                    error: function(){
                        console.log('failed to fecth artcie'); 
                    }
                });

        });
    },

文章视图中的saveView()是:

saveView() in the Article view is:

    saveView: function(){

        var viewDetails = [];

        viewDetails.device_id = this.options.device_id;
        viewDetails.article_id = this.model.id;
        viewDetails.project_title = project_title;

        var article_view = new models.ArticleView();

        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);
                            },
                        });
    },

这会点击REST API,记录文章的查看然后返回一些看不见的文章。这导致控制台输出:

This hits a REST API, records the viewing of the article and then returns a count of unseen Articles. This results in a console output of:


当和数据是router.js时:286 undefined router.js:287 in
saveView()成功和计数是Article.js:45 4

in when and data is router.js:286 undefined router.js:287 in saveView() success and count is Article.js:45 4

所以,不知何故, $。 无效,因为在执行 .done 脚本之前,它没有等待Ajax请求发送。有任何想法吗?

So, somehow, $.when is not working as it's not waiting for the Ajax request to send before executing the .done script. Any ideas?

推荐答案

你需要返回一个jQuery 延迟 对象 $。当正常工作时:

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的 save()方法返回 jqXHR 对象,其行为与与此情况下的 Deferred 对象相同。如上所述,只需将回复链接起来。这应该得到 $。when()等待请求完成。

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.

这篇关于$。当不等待Ajax请求完成时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆