自定义Ajax Promise函数签名 [英] Custom ajax promise function signature
问题描述
我想基本上为ajax done
和 fail
处理程序创建一个新的函数签名...鉴于下面的简化示例,我只是尝试附加 resource
作为要发送到处理程序的最后一个参数.
I want to basically create a new function signature for ajax done
and fail
handlers...given the simplified example below, I'm just trying to append resource
as the last parameter to be sent to the handler.
const customAjax = function( url, resource ) {
const p = $.ajax({
converters: {
'text script': function (text: string) {
return text;
}
},
url: url + "?" + resource
}).then(
function () {
[].push.apply(arguments, [resource]);
return arguments;
},
function() {
[].push.apply(arguments, [resource]);
return arguments;
}
);
return p;
};
然后首选用法如下:
customAjax( "url", "resourceKey" )
.done( function( data, textStatus, jqXHR, resourceName ) { /* do something */ } )
.fail( function( jqXHR, textStatus, errorThrown, resourceName ) { /* do something */ } );
问题是,在两个处理程序中,只有一个参数.javascript arguments
对象.因此,要访问我真正想要的每个参数,我必须执行以下操作:
The problem is, in both handlers, there is only one parameter. The javascript arguments
object. So, to access each parameter I really want, I have to do something like this instead:
customAjax( "url", "resourceKey" )
.done( function( args ) {
var data = args[ 0 ];
var textStatus = args[ 1 ];
var jqXHR = args[ 2 ];
var resourceName = args[ 3 ];
/* do something */
} )
.fail( function( args ) {
var jqXHR = args[ 0 ];
var textStatus = args[ 1 ];
var errorThrown = args[ 2 ];
var resourceName = args[ 3 ];
/* do something */
} );
有什么方法可以使其按我喜欢的方式工作?
Is there any way to make it work the way I'd prefer?
更新-不知道如何与关闭我问题的人进行沟通,但是我看不到引用的重复"问题如何回答/解决我的问题.
Update - Not sure how to communicate with person that closed my question, but I don't see how the referenced 'duplicate' question answers/addresses my issue.
推荐答案
您需要覆盖 $的对象的
返回,因此要确保随后使用额外的参数调用最终传递给这两个方法的回调: fail
和 done
方法属性.ajax
You would need to overwrite the fail
and done
method properties of the object that $.ajax
returns, so to make sure that the callback that is eventually passed to these two methods is then called with the extra argument:
const customAjax = function( url, resource ) {
const p = $.ajax({
converters: {
'text script': function (text: string) {
return text;
}
},
url: url + "?" + resource
});
let result = {
...p,
done(f) {
p.done(function (...args) {
f.call(this, ...args, resource);
});
return result;
},
fail(f) {
p.fail(function (...args) {
f.call(this, ...args, resource);
});
return result;
}
};
return result;
};
演示:
const customAjax = function( url, resource ) {
const p = $.ajax({
converters: {
'text script': function (text) {
return text;
}
},
url: url + "?" + resource
});
let result = {
...p,
done(f) {
p.done(function (...args) {
f.call(this, ...args, resource);
});
return result;
},
fail(f) {
p.fail(function (...args) {
f.call(this, ...args, resource);
});
return result;
}
};
return result;
};
customAjax( "https://jsonplaceholder.typicode.com/todos/1", "resourceKey" )
.done( function( data, textStatus, jqXHR, resourceName ) {
console.log( {data, textStatus, jqXHR, resourceName});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
这篇关于自定义Ajax Promise函数签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!