我有以下内容(来自 Tridion PowerTools ),它在运行某些 JavaScript 时从 CoreService 获取用户名。
JavaScript(安圭拉):
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);
};
核心服务端:(C# .svc)
[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json)]
public ExampleData GetUserInfo()
{
var coreService = Client.GetCoreService();
_exampleData = new ExampleData()
{
UserName = coreService.GetCurrentUser().Title
};
return _exampleData;
}
这会发送一个异步调用:
PowerTools.Model.Services.Example.GetUserInfo(onSuccess, onFailure, context, false)
而这分配了一个不同的函数来处理响应:
Function.getDelegate(this, this._handleUserInfo)
但是 onSuccess、onFailure、context 和 Boolean 是从哪里来的:
PowerTools.Model.Services.Example.GetUserInfo(onSuccess, onFailure, context, false)
?
此四参数签名与服务代码中的无参数 GetUserInfo() 不匹配。为什么是那个顺序和这四个?
请您参考如下方法:
onSuccess
和 onFailure
是分配用于处理来自 WCF 服务的响应的回调函数。
假设这是来自 PowerTools 项目的代码,则有一个自动生成的 JavaScript 方法充当 WCF 服务的代理方法(该服务的来源是 here )方法称为 GetUserInfo()
.
在那里,您实际上可以看到对 CoreService 的调用。这应该向您解释代理参数的映射。
onSuccess
是处理WCF服务响应的函数onFailure
是调用失败时运行的函数 context
是一个将传递回回调函数的变量,因此您可以使用它来传递信息。 false
是调用是否同步如果您的 WCF 服务要接受参数,则生成的代理将形成不同的签名,例如
PowerTools.Model.Services.Example.GetOtherInfo(param1, param2, onSuccess,
onFailure, context, false);