简体   繁体   中英

Call a javascript function from outside its object

I have some simple javascript that as far as i can tell should work but doesn't.

The code is below

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

this is part of the class Ajax.Net produces.

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));
} ...

Any help would be much appriciated; Colin G

The first parameter that needs to be passed to Function.call() is the object on which the function is called. Then follow the function parameters as separate values:

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

To call a function with an array of arguments you should use apply() . apply() also takes the object for which the method should be called as first parameter:

func.apply(someobj, params);

So in your case it would look something like this:

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

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

You need to pass an object to the first argument of the call method eg:

ajaxMethod.call(presenter, parameters);

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

Supertux is right. You could try this to make sure the context is set for "call":

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

Or you could simplify things quite a bit by not using 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM