[英]AJAX promise call handling
我有一个如下的Ember许诺电话;
var promise = new Ember.RSVP.Promise(function(resolve, reject) {
return $.ajax({
//want this common
url: requestUrl,
type: type, // HTTP method
dataType: dataType, // type of data expected from the API response
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(postData)
})
.done(function(data, status, xhrObject) {
//But want this to be different
// resolve call
})
.fail(function(xhrObject, status, error){
// reject call
});
})
我的问题是我可以为$ .ajax()使用通用代码,但对done()回调使用不同的实现吗,我可以通过从调用位置传递一些参数来进行检查。
所以基本上,我要
if (someparam == 'handleDone1')
call resolve(data)
else
call resolve({data})
您正在传递一个函数来done
硬编码函数表达式进去。
将其替换为变量。 将值作为函数参数传递给该变量。
或者,根本不要done
此处使用“ done
。 只需返回$.ajax()
的返回值,然后在调用函数中对该函数调用done()
。
返回一个promise,而不是ajax调用。 并将ajax调用包装为promise。
签出以下代码。 这可能会有所帮助。
function someFunction(resolve1, reject1) {
return new Ember.RSVP.Promise(function(resolve, reject) {
$.ajax({
//want this common
url: requestUrl,
type: type, // HTTP method
dataType: dataType, // type of data expected from the API response
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(postData)
})
.done(function(data, status, xhrObject) {
//But want this to be different
// resolve call
var dicision = resolve1();
if(dicision){
resolve(data);
} else {
resolve({data});
}
})
.fail(function(xhrObject, status, error){
// reject call
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.