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

2

6

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

Fireguard answered 9/12, 2010 at 12:34 Comment(0)
M
13

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.
Mating answered 9/12, 2010 at 13:43 Comment(3)
could you please be more elaborate??? I mean, what is the methodName parameter in OnSuccess?????Charmeuse
@JacksonLopes - I've added in a bit more info.Mating
why is it not working for me? the value of context is null for meDeliberate
E
4

You could use an anonymous function

PageMethod.SomeMethod(param1,..., function(){onSuccess(foo, importantValue)}, onFailure)
Escort answered 9/12, 2010 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.