$.ajax的事件,如何创建回调?

发布于 2024-12-18 08:14:07 字数 1795 浏览 1 评论 0原文

我知道标题不是很直观,所以我将尝试详细说明我的问题:

我正在创建一个 jQuery 插件来进行异步调用。 有了它,我可以使用以下命令发布帖子:

$("form").bindAjaxPost "post"

问题是我想为这些事件创建回调(beforeSendcompletesuccess, error),例如,该命令将类似于:

$("form").bindAjaxPost "post", 
    success: (data) -> console.log "user success #{data}"
    beforeSend: () -> console.log "user beforeSend"

请注意,事件 beforeSendsuccess 是由插件的用户以及插件代码的内部:参见代码:

方法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 只接受一个函数。

我还尝试访问 ajaxUserOptionsajaxUserOptions 在事件 successbeforeSend... 中不可用,因为这些调用是异步的。

感谢大家的帮助!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的心爱 2024-12-25 08:14:07

您可能会发现使用 jQuery 1.5 中添加到 $.ajax 返回值的 Promise 接口更容易实现这一点。这样,您可以附加多个 completesuccesserror 回调(最好是 alwaysdonefail,因为以前的名称在 jQuery 1.8 中将被弃用。)因此您的代码可能会变成

    post: (ajaxUserOptions = {}) -> 

        defaultAjaxOptions =
            type: "POST"
            url: @options.url
            data: $(@form).serialize()
            dataType: @options.dataType

        jqXHR = $.ajax($.extend {}, ajaxUserOptions, defaultAjaxOptions)
        jqXHR.always pluginAlways
        jqXHR.always ajaxUserOptions.always if ajaxUserOptions.always
        # and likewise for fail and done

beforeSend 是一个更棘手的情况,因为您可以'不要这样做jqXHR 对象,但这只是将插件的函数和用户的函数包装在单个函数中的问题,其中,如果任一函数返回 false,则外部函数会执行此操作(因为这会停止 Ajax 请求):

beforeSend = ->
  return false if pluginBeforeSend() is false
  return false if ajaxUserOptions?.beforeSend() is false

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 multiple complete, success, and error callbacks (preferably as always, done, and fail, as the former names will be deprecated in jQuery 1.8.) So your code might become

    post: (ajaxUserOptions = {}) -> 

        defaultAjaxOptions =
            type: "POST"
            url: @options.url
            data: $(@form).serialize()
            dataType: @options.dataType

        jqXHR = $.ajax($.extend {}, ajaxUserOptions, defaultAjaxOptions)
        jqXHR.always pluginAlways
        jqXHR.always ajaxUserOptions.always if ajaxUserOptions.always
        # and likewise for fail and done

beforeSend 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 returns false, the outer function does so (since this stops the Ajax request):

beforeSend = ->
  return false if pluginBeforeSend() is false
  return false if ajaxUserOptions?.beforeSend() is false
江南月 2024-12-25 08:14:07

您能否从插件内部调用用户提供的回调(如果存在)?与此类似:

post: (ajaxUserOptions)-> 

  ajaxOptions = 
    type: "POST"
    url: @options.url
    data: $(@form).serialize()
    dataType: @options.dataType

    beforeSend: ->
      console.log "plugin beforeSend"
      ajaxUserOptions.beforeSend() if ajaxUserOptions?.beforeSend?

    complete: ->
      console.log "plugin complete"

    success: (data) ->
      console.log "plugin success"
      ajaxUserOptions.success(data) if ajaxUserOptions?.success?

    error: (request) ->
      console.log "plugin error"

    $.ajax(ajaxOptions)

Could you call the user supplied callbacks from inside your plug-in if they exist? Similar to this:

post: (ajaxUserOptions)-> 

  ajaxOptions = 
    type: "POST"
    url: @options.url
    data: $(@form).serialize()
    dataType: @options.dataType

    beforeSend: ->
      console.log "plugin beforeSend"
      ajaxUserOptions.beforeSend() if ajaxUserOptions?.beforeSend?

    complete: ->
      console.log "plugin complete"

    success: (data) ->
      console.log "plugin success"
      ajaxUserOptions.success(data) if ajaxUserOptions?.success?

    error: (request) ->
      console.log "plugin error"

    $.ajax(ajaxOptions)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文