我使用以下代码检查某些数据是否已存在于我的数据库中,然后我想将新的数据数组(不在我的数据库中)传递到下一页。在所有查询完成之前,我的承诺似乎已经解决了......
任何想法?
CheckInDB()
.then(
function () {
SetupStep3();
});
function CheckInDB() {
var q = $.Deferred();
NewsReleasesToAdd = JSON.parse(window.localStorage.getItem("NewsReleasesToAdd"));
$(NewsReleasesToAdd).each(function (i, item) {
DoCheck(item);
});
q.resolve();
return q.promise();
}
function DoCheck(item) {
var q = $.Deferred();
var file = '/api/newsreleases/?action=Find&Limit=0&find=' + item.url;
GetGenericJSONData(file, function (data) {
if (data.length == 0) {
NewsReleases.push(item);
}
q.resolve();
});
return q.promise();
}
function GetGenericJSONData(url, callbackFunction) {
$.getJSON(url, function (data) {
return callbackFunction(data);
}).error(function (jqXHR, textStatus, errorThrown) {
var responseText = jQuery.parseJSON(jqXHR.responseText);
alert(responseText);
});
}
function SetupStep3() {
$("#statusupdate").text(NewsReleases.length + " Database rows loaded - Continue to Step 3.");
$("#statusupdate").attr("href", "/collaboration/views/admin/newsreleases?step=3")
window.localStorage.setItem("NewsReleases", JSON.stringify(NewsReleases));
}