Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we pass multiple parameters to onSuccess method of PageMethod?

I'm calling PageMethod "SameMethod" from javascript method "caller" so that I can get some values from DB. After I get values, control is continuing in "onSuccess" method. Problem is that I need to use some variable values ("importantValue") from javascript method "caller" in "onSuccess" method.

 
function caller(){
    var importantValue = 1984;   
    PageMethod.SomeMethod(param1,..., onSuccess, onFailure)
}

onSuccess method should be something like this:

function onSuccess(pageMethodReturnValue, importantValue ){

}

Is it possible and, if it is, how to pass multiple parameters (besides return values of page method) to "onSuccess" method of PageMethod?

Thanks for help

like image 560
Janko Avatar asked Dec 09 '10 12:12

Janko


2 Answers

Pass your importantValue as an additional parameter when calling the PageMethod. (this is usually called the context parameter if you are searching online for more info)

function caller(){
    var importantValue = 1984;   
    PageMethod.SomeMethod(param1,..., onSuccess, onFailure, importantValue)
}

Then you can access the value in the onSuccess callback as follows:

function onSuccess(pageMethodReturnValue, context, methodName){
    // context == 1984
}

Update to explain onSuccess parameters for @JacksonLopes There is a good description on the aspalliance website in an article by Suresh Kumar Goudampally

The important bit (modified to use my parameter names) is:

The success call back method has three parameters:

  • pageMethodReturnValue - Returns the output of the page method.
  • context - This is used to handle different logic when single callback is used for multiple page method requests. We can also pass an array of values as the context parameter.
  • methodName - This parameter returns the name of page method called.
like image 84
Geoff Appleford Avatar answered Nov 19 '22 13:11

Geoff Appleford


You could use an anonymous function

PageMethod.SomeMethod(param1,..., function(){onSuccess(foo, importantValue)}, onFailure)
like image 4
Dawn Avatar answered Nov 19 '22 11:11

Dawn