我正在用 javascript 和 skydrive API 编写一个网站。
这是我的代码:
var clientId = '00000000400FFB5A';
var redirectUri = "http://milmantasimmigration.com/skydrive/test/test.html";
WL.init({
client_id: clientId,
redirect_uri: redirectUri
});
WL.Event.subscribe("auth.login", onLogin);
WL.Event.subscribe("auth.sessionChange", onSessionChange);
var session;
var companyFolders;
var reportFolders;
var reports;
session = WL.getSession();
if (session) {
log("You are already signed in!");
getCompanyFolders();
} else {
WL.login({
scope: "wl.signin"
});
}
function onLogin() {
var session = WL.getSession();
if (session) {
log("You are signed in!");
}
}
function onLogout() {
WL.logout();
log("You are logged out!");
}
function onSessionChange() {
var session = WL.getSession();
if (session) {
log("Your session has changed.");
}
}
function getCompanyFolders() {
var getCompanyFoldersDeferred = $.Deferred();
WL.api({
path: "/me/skydrive/shared",
method: "GET"
}).then(
function(response) {
companyFolders = [];
reportFolders = [];
reports = [];
for (var i = 0; i < response.data.length; i++) {
if (response.data[i].type == "folder") {
companyFolders.push(response.data[i]);
getReportFolders(response.data[i].id + "/files/", companyFolders.length - 1);
}
}
$.when.apply($, companyFolders).done(function() {
getCompanyFoldersDeferred.resolve();
});
}, function(response) {
log("Cannot get files and folders: " + JSON.stringify(response.error).replace(/,/g, ",\n"));
});
return getCompanyFoldersDeferred.promise();
}
function getReportFolders(path, index1) {
var getReportFoldersDeferred = $.Deferred();
//var index2 = 0;
WL.api({
path: path,
method: "GET"
}).then(
function(response) {
reportFolders[index1] = [];
reports[index1] = [];
for (var i = 0; i < response.data.length; i++) {
if (response.data[i].type == "folder") {
reportFolders[index1].push(response.data[i]);
//index2++;
getReports(response.data[i].id + "/files/", index1, reportFolders[index1].length - 1);
}
}
$.when.apply($, reportFolders).done(function() {
getReportFoldersDeferred.resolve();
});
}, function(response) {
log("Cannot get files and folders: " + JSON.stringify(response.error).replace(/,/g, ",\n"));
});
return getReportFoldersDeferred.promise();
}
function getReports(path, index1, index2) {
var getReportsDeferred = $.Deferred();
//var index3 = 0;
WL.api({
path: path,
method: "GET"
}).then(
function(response) {
reports[index1][index2] = [];
for (var i = 0; i < response.data.length; i++) {
if (response.data[i].type == "file") {
reports[index1][index2].push(response.data[i]);
//index3++;
}
}
$.when.apply($, reports).done(function() {
getReportsDeferred.resolve();
});
}, function(response) {
log("Cannot get files and folders: " + JSON.stringify(response.error).replace(/,/g, ",\n"));
});
return getReportsDeferred.promise();
}
getCompanyFolders().done(function() {
showReportsHTML();
});
function showReportsHTML() {
for (var i = 0; i < companyFolders.length; i++) {
for (var j = 0; j < reportFolders[i].length; j++) {
for (var k = 0; k < reports[i][j].length; k++) {
alert(reports[i][j][k].name);
}
}
}
}
function log(x) {
alert(x);
}
这两行
getCompanyFolders();
showReportsHTML();
getCompanyFolders() 函数完成其工作后,应该一个接一个地执行函数。但 showReportsHTML() 在 getCompanyFolders() 填充报告 3D 数组之前执行。
getCompanyFolders() 完成后如何启动 showReportsHTML() 函数?