【问题标题】:Javascript global array undefined when in OnSuccess function of PageMethod?在PageMethod的OnSuccess函数中未定义Javascript全局数组?
【发布时间】:2011-07-28 17:39:09
【问题描述】:

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

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

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

【问题讨论】:

  • 我找到了this,但我不确定为什么当 OnSuccess 函数名称作为参数提供给 pagemethod 调用时,闭包不起作用?

标签: javascript asynchronous callback closures pagemethods


【解决方案1】:

数组在 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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 2018-08-06
    • 2011-08-12
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多