首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Worklight 6.2中链接来自移动客户端的适配器调用?

如何在Worklight 6.2中链接来自移动客户端的适配器调用?
EN

Stack Overflow用户
提问于 2015-02-17 16:12:48
回答 1查看 196关注 0票数 2

有人能解释一下如何使用Worklight 6.2链接适配器调用吗?

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

我可以把所有的调用放到一个堆栈中,然后依次调用每个调用,但是它们似乎不需要等待下一个调用开始之前完成?

我目前掌握的代码如下:

代码语言:javascript
运行
复制
// 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是不受欢迎的,但就目前而言,这是我设法以正确顺序执行的最接近的调用。

EN

回答 1

Stack Overflow用户

发布于 2015-02-17 16:24:17

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

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

因此,您的代码应该如下所示:

代码语言:javascript
运行
复制
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支持承诺,所以这将是我建议的处理代码的方式。

代码语言:javascript
运行
复制
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调用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28565860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档