I know that the title was not very intuitive, so I will try to detail my problem:
I am creating a jQuery plugin to create asynchronous calls. With it, I can, for example, make a message 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 will look something like this:
$("form").bindAjaxPost "post", success: (data) -> console.log "user success #{data}" beforeSend: () -> console.log "user beforeSend"
Please note that the beforeSend
and success
event was defined by the plugin user, as well as internally in the plugin code: see code:
Post jquery plugin method
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 success of the event to be called internally, and then the custom event plugin. (this sequence)
Execution example
Command
$("form").bindAjaxPost "post", success: (data) -> console.log "user success #{data}" beforeSend: () -> console.log "user beforeSend"
Output
beforeSend plugin user beforeSend plugin success user success
Attempts
As you can see in the code, I tried using the extend
jQuery command. But this did not work, because $.ajax().beforeSend
accepts only one function.
I also tried to access ajaxUserOptions
, but ajaxUserOptions
not available in success
, beforeSend
... events, since these calls are asynchronous.
Thank you all for your help!