Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do all these parameters when calling a WCF web method from Anguilla JavaScript mean?

Tags:

tridion

I have the following (from Tridion PowerTools), which gets a user name from the CoreService when some JavaScript runs.

JavaScript (Anguilla):

PowerTools.Popups.Example.prototype._onbtnGetUserInfoClicked = function () { 
    var onSuccess = Function.getDelegate(this, this._handleUserInfo);
    var onFailure = null;
    var context = null;
    //call function
    PowerTools.Model.Services.Example.GetUserInfo(onSuccess, onFailure, 
                                                  context, false);
}; 

// Delegate function "onSuccess"
PowerTools.Popups.Example.prototype._handleUserInfo = function (response) {
    var p = this.properties;
    $j("#lblUserInfo").text(response.UserName);
};

CoreService side: (C# .svc)

[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json)]
public ExampleData GetUserInfo()
{
    var coreService = Client.GetCoreService();
    _exampleData = new ExampleData()
    {
        UserName = coreService.GetCurrentUser().Title
    };
    return _exampleData;
}

This sends an asynchronous call:

PowerTools.Model.Services.Example.GetUserInfo(onSuccess, onFailure, context, false)

Whereas this assigns a different function to handle the response:

Function.getDelegate(this, this._handleUserInfo)

But where does onSuccess, onFailure, context, and the Boolean come from in: PowerTools.Model.Services.Example.GetUserInfo(onSuccess, onFailure, context, false)?

This four-parameter signature doesn't match the no-paramater GetUserInfo() in the service code. Why that order and these four?

like image 779
Alvin Reyes Avatar asked Feb 21 '12 22:02

Alvin Reyes


1 Answers

The onSuccess and onFailure are the callback functions that are assigned for handling the response from the WCF service.

Assuming this is code from the PowerTools project, there is an auto-generated JavaScript method that acts as a proxy method for a WCF service (source of the service is here) method called GetUserInfo().

In there you can actually see the call to the CoreService. That should explain to you the mapping of the proxy parameters.

  1. onSuccess is the function to process the response of the WCF service
  2. onFailure is the function to run if the call fails
  3. context is a variable that will be passed back into your callback functions, so you can use it to pass things around.
  4. false is whether the call is synchronous or not

If your WCF service were to take parameters, the generated proxy would form a different signature, something like

PowerTools.Model.Services.Example.GetOtherInfo(param1, param2, onSuccess, 
                                               onFailure, context, false);
like image 106
Chris Summers Avatar answered Oct 20 '22 22:10

Chris Summers