有人可以解释一下如何使用 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 已被弃用,但目前,这是我设法让调用以正确的顺序执行的最接近的结果。
最佳答案
通过使用 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
是您将传递给函数的参数数组。在这种情况下,youDataCollectionArray
和defCollection
应该具有相同的长度
更新:
WL.Client.invokeProcedure
支持 Promise,因此这将是我推荐的处理代码的方式
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
完成后调用 lastAdapterInitation
。 lastAdapterInspiration
将调用您提到的最后需要调用的适配器,您需要以与实现 sendUpdate
相同的方式实现该函数。
请记住,如果您愿意,您可以在中间链接更多对 sendUpdate
的调用。
关于javascript - 如何在 Worklight 6.2 中链接来自移动客户端的适配器调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28565860/