简体   繁体   中英

Coffeescript (jQuery) in Rails app doesn't work?

I am struggling with implementing a functionality with jQuery + Coffeescript - the problem is that the code doesn't show any alert windows.

Here's the JS functionality:

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  subscription.setupForm()

subscription =
  setupForm: ->
    $('#new_subscription').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        subscription.processCard()
        false
      else
        true

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, subscription.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#subscription_stripe_card_token').val(response.id)
      $('#new_subscription')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

All class and id attributes are configured properly, but when I click the button, no alert window is called.

When I put an alert window before the jQuery -> line,

alert('asf')
jQuery ->
...

the alert window is displayed, but when I do

jQuery ->
alert('asf')
...

then is not displayed.

The file above is successfully loaded. I am using jQuery v1.11.1 , Rails v4 .

What am I overlooking here?

Thank oyu

Intendation is important in coffescript

jQuery ->
  alert('asf')

will produce

 jQuery(function() {
   return alert('asf');
 });

However

jQuery ->
alert('asf')

Will produce

jQuery(function() {});
alert('asf');

Perhaps screwed up some other intendations. Check the compiled js code with chrome dev tools of Firebug or the like

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