我有一个小函数用于处理 ajax 请求并从中获取数据。
一旦我的所有 JSON 被聚合到一个数组中,我就会触发一个事件。
我的问题是我的变量 mySources 是一个数组,在处理过程中被修改了。在事件触发之前,正如预期的那样,它是一个由 4 个数组组成的数组,但在被“监听”之后它只是一个数组(甚至不是一个数组中的一个数组,而只是第一个数组)
function setSources(){
var deffereds = [];
if (arguments.length == 0)
{
deffereds.push(getTweets()),
deffereds.push(getFacebookstatuses()),
deffereds.push(getCampaigns()),
deffereds.push(getArticles())
}
else
{
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] == 'tweet'){
deffereds.push(getTweets())
}
else if (arguments[i] == 'facebookstatus'){
deffereds.push(getFacebookstatuses())
}
else if (arguments[i] == 'campaign'){
deffereds.push(getCampaigns())
}
else if (arguments[i] == 'article'){
deffereds.push(getArticles())
}
}
}
$.when.apply(null,deffereds).done(function(){
var mySources = new Array();
for (var i = 0; i < arguments.length; i++) {
mySources[i]=$.parseJSON(arguments[i][2].responseText).objects;
};
console.log(mySources); **// This gives me an array of 4 arrays as expected**
$(document).trigger('cal/results',mySources);
});
$(document).on('cal/mySources', function(e,mySources){
console.dir(mySources); **// This gives me only the first of the 4 arrays**
});
};