Javascript 如何在Worklight 6.2中链接来自移动客户端的适配器调用?
有人能解释一下如何使用Worklight 6.2链接适配器调用吗 我目前正在使用Worklight开发一个混合移动应用程序,我遇到的问题是,我需要对特定Worklight适配器进行x次调用,堆栈中的最后一次调用始终是对不同的Worklight适配器。每个适配器调用都需要等待上一次调用的结果,然后才能启动 我可以将所有调用放入一个堆栈中,并依次调用每个调用,但它们似乎不会等到上一个调用完成后再开始下一个调用 我现时的守则如下:Javascript 如何在Worklight 6.2中链接来自移动客户端的适配器调用?,javascript,ibm-mobilefirst,worklight-adapters,Javascript,Ibm Mobilefirst,Worklight Adapters,有人能解释一下如何使用Worklight 6.2链接适配器调用吗 我目前正在使用Worklight开发一个混合移动应用程序,我遇到的问题是,我需要对特定Worklight适配器进行x次调用,堆栈中的最后一次调用始终是对不同的Worklight适配器。每个适配器调用都需要等待上一次调用的结果,然后才能启动 我可以将所有调用放入一个堆栈中,并依次调用每个调用,但它们似乎不会等到上一个调用完成后再开始下一个调用 我现时的守则如下: // Following line is executed in a
// 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已被弃用,但就目前而言,这是我最接近于让调用以正确的顺序执行的方法。使用
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
是一个参数数组,您将传递给函数。在这种情况下,youdatacollectionaray
和defCollection
的长度应该相同
更新:
WL.Client.invokeProcess
支持承诺,因此这将是我推荐的处理代码的方法
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
两次和lastAdapterInternative
lastAdapterInvocation
将调用您提到的最后需要调用的适配器,您需要以实现sendUpdate
的相同方式实现该功能
请记住,如果你愿意,你可以在中间链接更多的代码到<代码> sEdDebug < /Cord>。Yoel,谢谢你的快速回复。我已经尝试了你的建议,调用是按正确的顺序执行的,但它们仍然不会在执行下一个调用之前等待上一个调用的响应?你有什么进一步的想法吗?嗨@ChrisP我在我的回答中添加了一个更新来演示承诺的使用