我有一个jquery-plugin,它可以处理一个函数dataSource
var plugin = $element.plugin({
data: function (callback) {
// TODO
}
});
如果我只使用一个dataSource,那就非常直接了...
但是......我需要两个dataSources
到目前为止我尝试过:
var plugin = $element.plugin({
data: function (callback) {
var path1 = 'foo1.json';
var path2 = 'foo2.json';
var resultOfPath1 = GetContent(path1);
var resultOfPath2 = GetContent(path2);
var result = /* this is where i am stuck !! */;
callback(result);
}
});
function GetContent(path) {
// this is where i am stuck!!
// which method to take? i would like to force a sync. request here
}
我对此代码的两个问题:
答案 0 :(得分:2)
var plugin = $element.plugin({
data: function (callback) {
var path1 = 'foo1.json';
var data1 = {};
var path2 = 'foo2.json';
var data2 = {};
$.getJSON(path1, data1, function (result1) {
$.getJSON(path2, data2, function (result2) {
var result = new Array();
jQuery.each(result1, function (index, element) {
result.push(element);
});
jQuery.each(result2, function (index, element) {
result.push(element);
});
callback(result);
}
});
}
});
答案 1 :(得分:1)
试试这个:
var plugin = $element.plugin({
data: function (callback) {
var
paths = ['foo1.json', 'foo2.json'],
result = [],
countState = 0;
$.each(paths, function() {
var dataSend = {}; //??
$.getJSON(this, dataSend, function(data) {
jQuery.each(data, function (i, el) {
result.push(el);
});
countState++;
if (countState == paths.length)
callback(result);
});
});
}
});
答案 2 :(得分:0)
有一个全局变量并推送你从ajax获得的数据。
var queue = [];
$.ajax({
url:'blabla',
success:function(data){
queue += data
}
});
$.ajax({
url:'blabla2',
success:function(data){
queue += data
}
})