有人能解释一下如何使用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是不受欢迎的,但就目前而言,这是我设法以正确顺序执行的最接近的调用。
发布于 2015-02-17 16:24:17
通过使用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
支持承诺,所以这将是我建议的处理代码的方式。
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
和调用lastAdapterInvocation
。lastAdapterInvocation
将调用您提到的适配器,最终需要调用该适配器,您需要以实现sendUpdate
的方式实现该函数。
请记住,如果您愿意,可以在中间链接更多的sendUpdate
调用。
https://stackoverflow.com/questions/28565860
复制相似问题