简体   繁体   中英

how to write this javascript function in coffeescript (ajax callbacks)

jQuery(function($) {
  // create a convenient toggleLoading function
  var toggleLoading = function() { $("#loading").toggle() };

  $("#new_post")
    .bind("ajax:loading",  toggleLoading)
    .bind("ajax:complete", toggleLoading)
    .bind("ajax:success", function(event, data, status, xhr) {
      $("#response").html(data);
    });
});

this is my js function (or Simone Carletti's ) and I want to transform it to coffeescript, I am having trouble with the last two callbacks, though.

My coffeescript looks like this

jQuery ->
    $("#new_post")
    .bind("ajax:loading",  toggleLoading)
    .bind("ajax:complete", toggleLoading)
    .bind("ajax:success", (event, data, status, xhr) ->
        alert(data)
    .bind("ajax:failure", (event, data, status, xhr) ->
        alert(data)

but I am getting an Error: unclosed INDENT on line 21

thanks in advance

The problem here is simply that you have mismatched parentheses. The line

.bind("ajax:success", (event, data, status, xhr) ->
    alert(data)

never closes the .bind call. Change your code to

jQuery ->
    $("#new_post")
    .bind("ajax:loading",  toggleLoading)
    .bind("ajax:complete", toggleLoading)
    .bind("ajax:success", (event, data, status, xhr) ->
        alert(data))
    .bind("ajax:failure", (event, data, status, xhr) ->
        alert(data))

and it'll work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM