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?
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.
onSuccess
is the function to process the response of the WCF serviceonFailure
is the function to run if the call fails context
is a variable that will be passed back into your callback functions, so you can use it to pass things around.false
is whether the call is synchronous or notIf 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With