Javascript 全局数组在 PageMethod 的 OnSuccess 函数中未定义?

发布于 2024-11-26 23:39:57 字数 1667 浏览 1 评论 0原文

我有多个用户控件使用页面方法异步加载到我的页面上。某些下拉列表会导致这些用户控件异步回调,并且用户控件会重新加载修改后的内容。我们这样做有我们的理由。

但是,目前我们的用户必须等待这些用户控件加载才能更改下拉列表中的选择。为了提供更好的用户体验,我尝试中止之前尚未加载的请求。

我试图维护一个未完成的异步请求执行器的全局 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
    }
}

I have multiple user controls loading asynchronously on my page, using pagemethods. Some dropdownlists cause asynchronous callbacks of those usercontrols, and the user controls are reloaded with modified content. We had our reasons for doing it this way.

However, currently our users have to wait for those user controls to load before they can change their selection in the dropdownlists. In an attempt to provide a better user experience, I'm attempting to abort previous, yet-to-be-loaded, requests.

I'm attempting to maintain a global js array of outstanding asynchronous request executors, in order to ensure that only the latest request for each user control is loaded. In other words, I want to cancel previous requests for a specific usercontrol which have yet to load and give priority to the latest request for that usercontrol.

The problem I have is that by the time execution of my OnSuccess function happens, the global array is undefined.

Am I going about this in the wrong way? What is it that I don't know?

Any help would be much appreciated.

This is a pared down example of my code:

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

z祗昰~ 2024-12-03 23:39:57

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
    }
}

Arrays are pass by reference in js. This has the benefit of allowing me to see the array in the state it was in when I hit the OnSuccess function, not the state it was in when I called the pagemethod. At least I think that's why it works. I needed the reference to the array passed into the OnSuccess function. I ended up doing this with the last two functions shown above, which worked nicely..

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文