[英]Javascript global array undefined when in OnSuccess function of PageMethod?
我有多個用戶控件使用pagemethod異步加載到我的頁面上。 一些下拉列表會導致這些用戶控件的異步回調,並且用戶控件會重新加載修改后的內容。 我們有這樣做的理由。
但是,當前,我們的用戶必須等待這些用戶控件加載,然后才能在下拉列表中更改其選擇。 為了提供更好的用戶體驗,我試圖中止以前的尚未加載的請求。
我正在嘗試維護未完成的異步請求執行程序的全局js數組,以確保僅加載每個用戶控件的最新請求。 換句話說,我想取消對尚未加載的特定用戶控件的先前請求,並優先考慮對該用戶控件的最新請求。
我的問題是,在執行OnSuccess函數時,全局數組未定義。
我會以錯誤的方式處理嗎? 我不知道是什么?
任何幫助將非常感激。
這是我的代碼的簡化示例:
var outstanding_requests;
$.fn.remove_or_item = function (val, col) {
var arr1 = $(this);
var arr2 = new Array();
$.each(arr1, function (k, v) {
if (v[col] != val) { arr2.push(v); }
else if (v[1].get_started()) { v[1].abort(); }
});
return arr2;
}
$.fn.find_or_item = function (val, col) {
var item;
var arr1 = $(this);
$.each(arr1, function (k, v) {
if (v[col] == val) { item = v; return false; } return true;
});
return item;
}
function RunMyPageMethod(panelname) {
var request; //current request object
if (outstanding_requests == undefined) { outstanding_requests = new Array(); }
outstanding_requests = $(outstanding_requests).remove_or_item(panelname, 0);
request = PageMethods._staticInstance.LoadUserControl(panelname, PageMethodSuccess, PageMethodFailure);
outstanding_requests.push([panelname, request.get_executor()]);
}
function PageMethodSuccess(result, userContext, methodName) {
var panelname = result.split("|")[0];
//here outstanding_requests is undefined
if($(outstanding_requests).find_or_item(panelname,0))
{
outstanding_requests = $(outstanding_requests).remove_or_item(panelname, 0);
//load usercontrol
}
}
數組在js中通過引用傳遞。 這樣的好處是可以讓我看到按OnSuccess函數時的狀態的數組,而不是當我調用pagemethod時的狀態。 至少我認為這就是它起作用的原因。 我需要傳遞給OnSuccess函數的數組的引用。 我最后用上面顯示的最后兩個功能完成了此操作,效果很好。
function RunMyPageMethod(panelname) {
var request; //current request object
if (outstanding_requests == undefined) { outstanding_requests = new Array(); }
outstanding_requests = $(outstanding_requests).remove_or_item(panelname, 0);
request = PageMethods._staticInstance.LoadUserControl(panelname,function(result,userContext,methodName){ PageMethodSuccess(result,userContext,methodName,outstanding_requests); }, PageMethodFailure);
outstanding_requests.push([panelname, request.get_executor()]);
}
function PageMethodSuccess(result, userContext, methodName, outstanding_requests) {
var panelname = result.split("|")[0];
if($(outstanding_requests).find_or_item(panelname,0))
{
outstanding_requests = $(outstanding_requests).remove_or_item(panelname, 0);
//load usercontrol
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.