2013-03-06 80 views
1

对于我想要实现延迟加载的jQuery应用程序。 因此,我创建了一个对象,包含我所有的jqXHR promise。jQuery承诺不适用于多个jqXHR

当我现在组的所有成声明

var resultset = new Object(); 
resultset.one = $.getScript('http://......'); 
resultset.two = $.getScript('http://......'); 

$.when(resultset.one,resultset.two).then(
function(){ alert('success')}, 
function(){alert('failure')} 
); 

那么它总是转到错误状态。我不知道为什么,因为js调试器告诉,所有请求都很好(状态200)。

的JQ API文档告诉下面是去工作:

$.when($.ajax("/page1.php"), $.ajax("/page2.php")) 
    .then(myFunc, myFailure); 

什么想法?

回答

2

您可以使用getScript加入承诺(),并等待,直到所有的脚本加载,这样的:

$.when(
    $.getScript("/mypath/myscript1.js"), 
    $.getScript("/mypath/myscript2.js"), 
    $.getScript("/mypath/myscript3.js") 
).done(function(){ 

    //place your code here, the scripts are all loaded 

}); 
+0

你是对的,但后来我不能确定它们中的哪去了错误状态。我的事情是有用的一个有意义的错误消息。通过我之前发布的解决方案,您可以在之后观看您的承诺。 – redflag237 2013-03-06 14:14:34

0

好吧,我得到它的工作使用以下解决方法。 - >也许任何人都可以解释我的区别,请?

$ .when.apply($,(resultset.one,resultset.two))。then(...);