7

我有以下内容(来自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);
};

CoreService 端:(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() 不匹配。为什么那个顺序和这四个?

4

1 回答 1

7

onSuccessonFailure是分配用于处理来自 WCF 服务的响应的回调函数。

假设这是来自 PowerTools 项目的代码,则有一个自动生成的 JavaScript 方法充当 WCF 服务的代理方法(服务源在此处)方法称为GetUserInfo().

在那里,您实际上可以看到对 CoreService 的调用。那应该向您解释代理参数的映射。

  1. onSuccess是处理WCF服务响应的函数
  2. onFailure是调用失败时运行的函数
  3. context是一个变量,它将被传递回你的回调函数,所以你可以用它来传递东西。
  4. false是调用是否同步

如果您的 WCF 服务要接受参数,则生成的代理将形成不同的签名,例如

PowerTools.Model.Services.Example.GetOtherInfo(param1, param2, onSuccess, 
                                               onFailure, context, false);
于 2012-02-21T22:06:13.240 回答