我有一个函数,必须连接到DB才能获取上传令牌,然后上传文件,然后关闭流并将文件记录在DB中。我无法将所有这些链接在一起。
var saveNewRequest = function (image_arr) {
return $.ajax({
url: 'http://' + AppVar.ServerUrlWithPort + '/restapi/MtReqNewRequest_SaveData',
type: 'POST',
data: JSON.stringify({
'SessionId': AppVar.SessionId,
'Name': $('#MtReqNewRequest_name').val(),
'Desc': $('#MtReqNewRequest_desc').val(),
'Obj': $('#MtReqNewRequest_obj').val(),
'Priority': $('#MtReqNewRequest_priority2').val(),
'Status': $('#MtReqNewRequest_status2').val(),
'Type': $('#MtReqNewRequest_type2').val()
}),
dataType: 'json',
contentType: "application/json",
timeout: 10000
}).done(function (response) {
if (response.ResultCode === '0') {
if (image_arr.length != 0) {
//this is recursively called upload function which returns jQuery promise (this works as intended)
// the promise resolves with RequestId which I need later on
return uploadImages(image_arr, image_arr.length, 0, response.RequestId)
} else {
//I would like this to return just this RequestId
Promise.resolve(response.RequestId)
}
} else {
Promise.reject().promise();
}
}).fail(function (x, t, m) {
if (t === "timeout") {
reject("Timeout: " + t);
} else {
reject($.i18n('Error-RetrivingDataProblem'));
}
})
}
我在一个事件中称之为:
MtReq.saveNewRequest(image_arr).then(function (output) {
AppVar.nav.popPage().then(function () {
Utility.hideModalWithProgressBar();
if (!isNaN(output)) {
setTimeout(500, AppVar.nav.pushPage("MtReqRequestPage.html", { animation: "slide", id: output }));
}
})
}).catch(function (e) {
Utility.hideModalWithProgressBar();
ons.notification.alert(e);
})
我需要将RequestID传递给AppVar.nav.pushPage
,以打开我刚刚创建的页面。但是,我在saveNewRequest
中得到了第一个Ajax请求的完整响应。
这是Cordova app,使用OnsenUI框架(但这与问题无关)。另外,我使用最新的BluebirdJs作为Promise polyfill(据我所知,它应该使JS和jQuery promises兼容)。
感谢您的帮助!
答案 0 :(得分:1)
将.then()
替换为.done()
; .done()
返回$.ajax()
返回的相同jQuery promise对象。 return
Promise
或来自.then()
的其他值var saveNewRequest = function (image_arr) {
return $.ajax({
url: 'http://' + AppVar.ServerUrlWithPort + '/restapi/MtReqNewRequest_SaveData',
type: 'POST',
data: JSON.stringify({
'SessionId': AppVar.SessionId,
'Name': $('#MtReqNewRequest_name').val(),
'Desc': $('#MtReqNewRequest_desc').val(),
'Obj': $('#MtReqNewRequest_obj').val(),
'Priority': $('#MtReqNewRequest_priority2').val(),
'Status': $('#MtReqNewRequest_status2').val(),
'Type': $('#MtReqNewRequest_type2').val()
}),
dataType: 'json',
contentType: "application/json",
timeout: 10000
}).then(function (response) {
if (response.ResultCode === '0') {
if (image_arr.length != 0) {
//this is recursively called upload function which returns jQuery promise (this works as intended)
// the promise resolves with RequestId which I need later on
return uploadImages(image_arr, image_arr.length, 0, response.RequestId)
} else {
//I would like this to return just this RequestId
// added `return`
return Promise.resolve(response.RequestId)
}
} else {
// note `return`, removed `.promise()`
return Promise.reject()
}
}).fail(function (x, t, m) {
if (t === "timeout") {
// included `Promise`, chain `.reject()`
// note, `return`
return Promise.reject("Timeout: " + t);
} else {
// note `Promise.reject()`, added `return`
return Promise.reject($.i18n('Error-RetrivingDataProblem'));
}
})
}
。
package com.example.webviewdemo;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
private long touchDownMs;
private Handler handler;
private int numberOfTaps;
private long lastTapTimeMs;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public void newTouch(){
}
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
handler= new Handler();
if(event.getAction()== MotionEvent.ACTION_MOVE)
{
return true;
}
if(event.getAction()== MotionEvent.ACTION_DOWN){
touchDownMs = System.currentTimeMillis();
return v.onTouchEvent(event);
}
else if(event.getAction()== MotionEvent.ACTION_UP){
handler.removeCallbacksAndMessages(null);
if ((System.currentTimeMillis() - touchDownMs) > ViewConfiguration.getTapTimeout()) {
//it was not a tap
numberOfTaps = 0;
lastTapTimeMs = 0;
}
if (numberOfTaps > 0
&& (System.currentTimeMillis() - lastTapTimeMs) < ViewConfiguration.getDoubleTapTimeout()) {
numberOfTaps += 1;
} else {
numberOfTaps = 1;
}
lastTapTimeMs = System.currentTimeMillis();
if (numberOfTaps == 3) {
// Toast.makeText(getApplicationContext(), "triple", Toast.LENGTH_SHORT).show();
//handle triple tap
} else if (numberOfTaps == 2) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
//handle double tap
newTouch();
// Toast.makeText(getApplicationContext(), "double", Toast.LENGTH_SHORT).show();
}
}, ViewConfiguration.getDoubleTapTimeout());
}
return v.onTouchEvent(event);
}
else{
return v.onTouchEvent(event);
}
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}