SAPUI5处理带有承诺的循环 oModel.create(“/SurveySet”,oEntry,{ 成功:功能(oData){ 对于(var i=0;i
我正在创建一个SurveySet,它返回一个SurveyId 对于每个调查,我需要在For循环中创建一个问题集,每次返回数据进行额外处理 问题在于执行顺序。for循环在从第一次迭代检索oData之前递增 例如,如果for循环有0和1,则只执行最后一个元素1SAPUI5处理带有承诺的循环 oModel.create(“/SurveySet”,oEntry,{ 成功:功能(oData){ 对于(var i=0;i,odata,sapui5,Odata,Sapui5,我正在创建一个SurveySet,它返回一个SurveyId 对于每个调查,我需要在For循环中创建一个问题集,每次返回数据进行额外处理 问题在于执行顺序。for循环在从第一次迭代检索oData之前递增 例如,如果for循环有0和1,则只执行最后一个元素1 如何将for循环的增量延迟到为循环的第一次迭代返回oData之后?您可以将调查数据推送到数组中,然后执行for循环 oModel.create("/SurveySet", oEntry, { success: function(oD
如何将for循环的增量延迟到为循环的第一次迭代返回oData之后?您可以将调查数据推送到数组中,然后执行for循环
oModel.create("/SurveySet", oEntry, {
success: function(oData) {
for (var i = 0; i < questionData.questions.length; i++) {
var oEntry = {};
oEntry.SurveyId = oData.SurveyId;
oModel.create("/QuestionSet", oEntry, {
changeSetId: i.toString(),
success: function(oData) {
//Additional Processing
}
}
}
}
}
$。每个(数据、函数(索引、值){
对于(var i=0;i
您可以将测量数据推送到阵列,然后执行FOR循环
oModel.create("/SurveySet", oEntry, {
success: function(oData) {
for (var i = 0; i < questionData.questions.length; i++) {
var oEntry = {};
oEntry.SurveyId = oData.SurveyId;
oModel.create("/QuestionSet", oEntry, {
changeSetId: i.toString(),
success: function(oData) {
//Additional Processing
}
}
}
}
}
$。每个(数据、函数(索引、值){
对于(var i=0;i
我猜您可以在“附加处理”中访问oEntry
或I
问题是,带有附加处理的内部成功函数正在捕获外部定义的局部变量,如闭包中的i
和oEntry
。变量值不会被复制
i
,更改oEntry
,并执行oModel.create()
方法i
,更改oEntry
并再次执行oModel.create()
createSurvey:函数(oEntry){
var=这个;
oModel.create(“/SurveySet”),oEntry,{
成功:功能(oData){
对于(var i=0;i
PS:您也可以使用questionData.questions.forEach(function(question,i){…});
来获得相同的效果。这次匿名函数复制值
如果您需要维持严格的额外处理顺序,并将请求顺序发送到后端,我确实建议使用承诺:
createSurvey:函数(oEntry){
var=这个;
oModel.create(“/SurveySet”),oEntry,{
成功:功能(oData){
var promise=promise.resolve();
forEach(函数(问题,i){//复制局部变量
//连锁反应
promise=promise.then(function(){返回that.createQuestion(oModel,question,i,oData)});
});
promise.then(函数(){
//全部完成
})
.catch(函数(){
//某个地方出错。剩余未执行
})
}
}
}
createQuestion(oModel,question,i,survey){//现在返回一个承诺
var oEntry={};
oEntry.SurveyId=survey.SurveyId;
返回新承诺(函数(resolve,reject){//将UI5内容包装成承诺
oModel.create(“/QuestionSet”),oEntry,{
changeSetId:i.toString(),
成功:功能(oData){
//附加处理
决心(奥达塔);
},
错误:拒绝
}
});
}
我猜您可以在“附加处理”中访问oEntry
或I
问题是,带有附加处理的内部成功函数正在捕获外部定义的局部变量,如闭包中的i
和oEntry
。变量值不会被复制
i
,更改oEntry
,并执行oModel.create()
方法i
,更改oEntry
并再次执行oModel.create()
$.each(data, function(index, value) {
for (var i = 0; i < questionData.questions.length; i++) {
var oEntry = {};
oEntry.SurveyId = value.SurveyId;
oModel.create("/QuestionSet", oEntry, {
changeSetId: i.toString(),
success: function(oData) {
//Additional Processing
}
}
}
)};