$。当不等待Ajax请求完成时

编程入门 行业动态 更新时间:2024-10-26 16:23:56
本文介绍了$。当不等待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请求完成时

本文发布于:2023-11-06 09:59:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!