【问题标题】:Call a javascript function from outside its object从对象外部调用 javascript 函数
【发布时间】:2009-07-24 13:17:21
【问题描述】:

我有一些简单的 javascript,据我所知应该可以工作,但不能。

代码如下

var presenter = new Practicum.Web.TEI.StudentPlacement2009.CreateLetter_class(); //this is a class generated by Ajax.Net

function GetLetters() {
    var GetLettersParams = new Object();
    GetLettersParams.TemplateType = $('#LetterTypes').val();
    var letters = ajaxCall(presenter.GetLetters, GetLettersParams);
    createOptions('Templates', letters, 'Id', 'Name', true);
}

function ajaxCall(ajaxMethod, parameters) {
    var response = ajaxMethod.call(parameters); //fails here with the message in
    if (response.error != null) {
        alert('An error has occured\r\n' + response.error.Message);
        return;
    }
    return response.value;
}

这是 Ajax.Net 生成的类的一部分。

Practicum.Web.TEI.StudentPlacement2009.CreateLetter_class = function() {};
Object.extend(Practicum.Web.TEI.StudentPlacement2009.CreateLetter_class.prototype, Object.extend(new AjaxPro.AjaxClass(), {
GetLetterTypes: function() {
return this.invoke("GetLetterTypes", {}, this.GetLetterTypes.getArguments().slice(0));
},
GetDegrees: function() {
return this.invoke("GetDegrees", {}, this.GetDegrees.getArguments().slice(0));
},
GetLetters: function(getLettersParams) {
return this.invoke("GetLetters", {"getLettersParams":getLettersParams}, this.GetLetters.getArguments().slice(1));
} ...

任何帮助将不胜感激; 科林·G

【问题讨论】:

  • “不起作用”不是很有启发性。它应该怎么做?它有什么作用?它会抛出什么错误?
  • 考虑到你有 $('#LetterTypes').val();在您的代码中 - 您是否也在使用 JavaScript 框架?
  • @David Dorward sort 以为我把错误信息放在了帖子上。将编辑和添加未来的克莱尔

标签: javascript ajax ajax.net


【解决方案1】:

需要传递给Function.call()的第一个参数是调用函数的对象。然后将函数参数作为单独的值:

func.call(someobj, param1, param2, ...);

要调用带有参数数组的函数,您应该使用apply()apply() 也将调用方法的对象作为第一个参数:

func.apply(someobj, params);

所以在你的情况下,它看起来像这样:

function ajaxCall(ajaxMethod, obj, parameters) {
  var response = ajaxMethod.call(obj, parameters);
  // ...
}

var letters = ajaxCall(presenter.GetLetters, presenter, GetLettersParams);

【讨论】:

  • 干杯……比现在有意义。感谢您的解释
【解决方案2】:

您需要将一个对象传递给调用方法的第一个参数,例如:

ajaxMethod.call(presenter, parameters);

http://www.webreference.com/js/column26/call.html

【讨论】:

    【解决方案3】:

    Supertux 是对的。您可以尝试这样做以确保为“呼叫”设置了上下文:

    function GetLetters() {
        var GetLettersParams = new Object();
        GetLettersParams.TemplateType = $('#LetterTypes').val();
        var letters = ajaxCall(presenter.GetLetters, presenter, GetLettersParams);
        createOptions('Templates', letters, 'Id', 'Name', true);
    }
    
    function ajaxCall(ajaxMethod, context, parameters) {
        var response = ajaxMethod.call(context, parameters); //Call requires a context
        if (response.error != null) {
            alert('An error has occured\r\n' + response.error.Message);
            return;
        }
        return response.value;
    }
    

    或者你可以通过不使用ajaxCall来简化一些事情:

    function GetLetters() {
        var GetLettersParams = { 
                TemplateType: $('#LetterTypes').val() 
            },
            response = presenter.GetLetters(GetLettersParams);
    
        if (response.error != null) {
            alert('An error has occured\r\n' + response.error.Message);
            return;
        }
    
        createOptions('Templates', response.value, 'Id', 'Name', true);
    }
    

    【讨论】:

    • 创建ajaxCall函数的原因是为了减少javascript中代码的重复。我的一些页面有 10 次对服务器的调用,并且 ajaxCall 方法中的当前功能被复制到每个页面中。严重需要重构
    • 如果你有多个调用,那么你肯定会想要重构,但是你有没有看过 ajaxCall 在做什么?这是一个处理错误的好地方,但你真的想用 .NET 错误消息提醒用户吗?
    • 是的,我不想向客户端发送 .net 错误。有很多验证规则,我提出了一个含义完整的客户端异常以及所有其他 .net 错误带有一条消息,例如它是否继续联系技术支持类型的事物,其引用 ID 与异常日志相关。可能不是最佳做法,但会对替代方案感兴趣
    • 我们记录错误并显示一条消息,“发生意外错误。我们已收到通知,问题将很快得到解决。”不是很有用,但比查看技术错误消息或堆栈跟踪更能吸引用户...有一个问题,但我不必担心,因为有人会修复它。
    • 我们在应用程序中有一个地方,我们在服务器端调用第三方 Web 服务。此调用有时会超时,在这种情况下,我们会显示一条消息,例如“我们的请求在导入您的数据时超时。如果这种情况继续存在,请稍后再试。”我们给他们一个按钮,上面写着“立即重试”。在大多数情况下,我们可以像这样提供有用的错误消息。在发生真正未知错误的(希望如此)罕见的情况下,我们只能向他们保证会迅速解决。
    猜你喜欢
    • 1970-01-01
    • 2014-10-01
    • 2015-03-02
    • 2019-12-13
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    相关资源
    最近更新 更多