views:

317

answers:

3

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

+1  A: 

You need to pass an object to the first argument of the call method e.g.:

ajaxMethod.call(presenter, parameters);

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

Supertux
+1  A: 

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);
}
Prestaul
The reason for creating the ajaxCall function is to reduce the duplication in code in the javascript. some of my pages have 10 calls to the server and the current functionality in the ajaxCall method is copied in each one. Seriously needs refactoring
Colin G
If you have multiple calls then you would certainly want to refactor, but have you looked at what ajaxCall is doing? It is a good place for error handling, but do you really want to alert the user with a .NET error message?
Prestaul
True i dont want to send .net error to the client. there are a lot of validation rules that i raise a meaning full client exception with all other .net error come with a message like if it continues contact tech support type thing with a referance Id which relates to the exception log. Possibly not the best practice but would be interested in an alternative
Colin G
We log errors and display a message that says, "An unexpected error has occurred. We have been notified and the issue will be addressed shortly." Not very useful, but much more appealing to users than seeing technical error messages or stack traces... There was an issue, but I don't have to worry because someone is going to fix it.
Prestaul
We have one place in our application where we call a third-party web service on the server-side. This call occasionally times out and in that case we display a message that says something like, "Our request timed out while importing your data. If this continues please try again at another time." and we give them a button that says, "Retry Now". In most cases we can provide a useful error message like this. In the (hopefully) rare case where a truly unknown error has occurred we can only reassure them that it will be addressed quickly.
Prestaul
+1  A: 

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);
sth
Cheers sth than makes sence now. Thanks for the explination
Colin G