【问题标题】:How do I chain adapter calls from the mobile client in Worklight 6.2?如何在 Worklight 6.2 中链接来自移动客户端的适配器调用?
【发布时间】:2015-02-17 16:12:48
【问题描述】:

有人可以解释您如何使用 Worklight 6.2 链接适配器调用吗?

我目前正在使用 Worklight 开发一个混合移动应用程序,我遇到的问题是我需要对特定 Worklight 适配器进行 x 次调用,堆栈中的最后一个调用将始终是到不同的 Worklight 适配器。每个适配器调用都需要等待上一次调用的结果才能启动。

我可以将所有调用放入一个堆栈中,然后依次调用每个调用,但它们似乎没有在下一个开始之前等待前一个完成?

我目前的代码如下:

// Following line is executed in a loop to build the call stack
defCollection.push(sendUpdate(data));


// Following code executes the call stack
var deferred = $.Deferred();
var promise = deferred.promise();

$.each(defCollection, function(index, sndUpd) {
    WL.Logger.info("EXECUTING :: " + index);
    promise = promise.pipe(function() {return sndUpd;});
});

deferred.resolve();


// This is the Worklight adapter call
function sendUpdate(data){
    var params = data;

    var invocationData = {
        adapter : "live",
        procedure : "update",
        parameters : [params],
        compressResponse : true
    };

    WL.Client.invokeProcedure(invocationData, {
        onSuccess : updateSuccess,
        onFailure : updateFailure
    });
}

我知道 .pipe 已被弃用,但目前,这是我设法让调用以正确的顺序执行的最接近的方法。

【问题讨论】:

    标签: javascript ibm-mobilefirst worklight-adapters


    【解决方案1】:

    通过使用defCollection.push(sendUpdate(data));,您可以执行sendUpdate 函数并将其响应“输出”传递给defCollection.push()

    尝试使用defCollection.push(sendUpdate),然后调用promise = promise.then(function() {return sndUpd(yourDataObjectHere);});

    所以你的代码应该是这样的:

    var youDataCollectionArray = [];
    youDataCollectionArray.push(data);
    
    
    defCollection.push(sendUpdate);
    
    
    // Following code executes the call stack
    var deferred = $.Deferred();
    var promise = deferred.promise();
    
    $.each(defCollection, function(index, sndUpd) {
        WL.Logger.info("EXECUTING :: " + index);
        promise = promise.then(function() {return sndUpd(youDataCollectionArray[index]);});
    });
    
    deferred.resolve();
    
    
    // This is the Worklight adapter call
    function sendUpdate(data){
        var params = data;
    
        var invocationData = {
            adapter : "live",
            procedure : "update",
            parameters : [params],
            compressResponse : true
        };
    
        WL.Client.invokeProcedure(invocationData, {
            onSuccess : updateSuccess,
            onFailure : updateFailure
        });
    }
    

    其中youDataCollectionArray 是您将传递给函数的参数数组。在这种情况下youDataCollectionArraydefCollection 应该是相同的长度

    更新:

    WL.Client.invokeProcedure 支持承诺,因此这是我推荐的处理代码的方式

    sendUpdate(data).then(function(response){
    
      return sendUpdate(otherData);
    }).then(function(response){
    
      /*
       * this will be similar to sendUpdate but it will call different adapter
       * since you said the call last call will be to a different adapter.
       */
      return lastAdapterInvocation();
    }).then(function(response){
      // last's adapter success
    }).fail(function(errorResponse){
      // Failed to invoke adapter
    });
    
    
    function sendUpdate(data){
      var params = data;
    
      var invocationData = {
        adapter : "live",
        procedure : "update",
        parameters : [params],
        compressResponse : true
      };
    
      return WL.Client.invokeProcedure(invocationData);
    }
    

    在本例中,您将调用两次sendUpdate,并在第二次sendUpdate 完成后调用lastAdapterInvocationlastAdapterInvocation 将调用您提到的最后需要调用的适配器,您需要以与实现sendUpdate 相同的方式实现该功能。

    请记住,如果您愿意,您可以将更多的调用链接到中间的sendUpdate

    【讨论】:

    • 您好 Yoel,感谢您的快速回复。我已经尝试了您的建议,调用以正确的顺序执行,但是在执行下一个调用之前,他们仍然不等待上一个调用的响应?你有什么进一步的想法吗?
    • 嗨@ChrisP 我在我的答案中添加了一个更新来演示诺言的使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    相关资源
    最近更新 更多