$.ajax的事件,如何创建回调?
我知道标题不是很直观,所以我将尝试详细说明我的问题:
我正在创建一个 jQuery 插件来进行异步调用。 有了它,我可以使用以下命令发布帖子:
$("form").bindAjaxPost "post"
问题是我想为这些事件创建回调(beforeSend
、complete
、success
, error
),例如,该命令将类似于:
$("form").bindAjaxPost "post",
success: (data) -> console.log "user success #{data}"
beforeSend: () -> console.log "user beforeSend"
请注意,事件 beforeSend
和 success
是由插件的用户以及插件代码的内部:参见代码:
方法post jquery插件
post: (ajaxUserOptions)->
ajaxOptions =
type: "POST"
url: @options.url
data: $(@form).serialize()
dataType: @options.dataType
beforeSend: ->
console.log "plugin beforeSend"
complete: ->
console.log "plugin complete"
success: (data) ->
console.log "plugin success"
error: (request) ->
console.log "plugin error"
ajaxOptions = $.extend {}, ajaxUserOptions, ajaxOptions if ajaxUserOptions
$.ajax(ajaxOptions)
问题
我想事件成功,应该在内部调用,然后是用户定义的事件插件。 (此顺序)
执行示例
Command
$("form").bindAjaxPost "post",
success: (data) -> console.log "user success #{data}"
beforeSend: () -> console.log "user beforeSend"
Output
插件 beforeSend 用户发送前 插件成功 用户成功
尝试
正如你在代码中看到的,我尝试使用 jQuery 的命令extend
。但它不起作用,因为 $.ajax().beforeSend
只接受一个函数。
我还尝试访问 ajaxUserOptions
但 ajaxUserOptions
在事件 success
、beforeSend
... 中不可用,因为这些调用是异步的。
感谢大家的帮助!
I know that the title was not very intuitive, so I'll try to detail my problem:
I am creating a jQuery plugin to make asynchronous calls.
With it I can for example make a post using the following command:
$("form").bindAjaxPost "post"
The problem is that I would like to create callbacks for these events (beforeSend
, complete
, success
, error
), for example, the command would look something like:
$("form").bindAjaxPost "post",
success: (data) -> console.log "user success #{data}"
beforeSend: () -> console.log "user beforeSend"
Notice that the event beforeSend
and success
was defined by the user of the plugin and also internally in the plugin code: See the code:
Method post jquery plugin
post: (ajaxUserOptions)->
ajaxOptions =
type: "POST"
url: @options.url
data: $(@form).serialize()
dataType: @options.dataType
beforeSend: ->
console.log "plugin beforeSend"
complete: ->
console.log "plugin complete"
success: (data) ->
console.log "plugin success"
error: (request) ->
console.log "plugin error"
ajaxOptions = $.extend {}, ajaxUserOptions, ajaxOptions if ajaxUserOptions
$.ajax(ajaxOptions)
Problem
I would like the event success, should be called internally, and then the user-defined event plugin. (this sequence)
Execution example
Command
$("form").bindAjaxPost "post",
success: (data) -> console.log "user success #{data}"
beforeSend: () -> console.log "user beforeSend"
Output
plugin beforeSend
user beforeSend
plugin success
user success
Attempts
As you can see in the code, I tried to use the command extend
of jQuery. But it did not work because the $.ajax().beforeSend
accepts only one function.
I also tried to access the ajaxUserOptions
but ajaxUserOptions
is not available in the events success
, beforeSend
..., since these calls are asynchronous.
Thank you all for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会发现使用 jQuery 1.5 中添加到
$.ajax
返回值的 Promise 接口更容易实现这一点。这样,您可以附加多个complete
、success
和error
回调(最好是always
、done
和fail
,因为以前的名称在 jQuery 1.8 中将被弃用。)因此您的代码可能会变成beforeSend
是一个更棘手的情况,因为您可以'不要这样做jqXHR 对象,但这只是将插件的函数和用户的函数包装在单个函数中的问题,其中,如果任一函数返回false
,则外部函数会执行此操作(因为这会停止 Ajax 请求):You may find this to be easier to implement using the Promise interface added to the return value of
$.ajax
in jQuery 1.5. With that, you can attach multiplecomplete
,success
, anderror
callbacks (preferably asalways
,done
, andfail
, as the former names will be deprecated in jQuery 1.8.) So your code might becomebeforeSend
is a trickier case, since you can't do that on the jqXHR object, but it's just a matter of wrapping your plugin's function and the user's function in a single function where, if either function returnsfalse
, the outer function does so (since this stops the Ajax request):您能否从插件内部调用用户提供的回调(如果存在)?与此类似:
Could you call the user supplied callbacks from inside your plug-in if they exist? Similar to this: