0

我有一些简单的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

4

3 回答 3

2

需要传递给的第一个参数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);
于 2009-07-24T14:01:25.160 回答
1

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

ajaxMethod.call(presenter, parameters);

请参阅http://www.webreference.com/js/column26/call.html

于 2009-07-24T13:30:44.527 回答
1

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);
}
于 2009-07-24T13:56:38.037 回答