简体   繁体   中英

How to check syntax error in coffeescript

I am not familiar with coffee script, I try to move jquery from view to put in asset but not able to make it work.

Here the working from view:

- jquery_ready do
  $('label[for=voucher_name], input#voucher_name').hide();
  $( "#voucher_voucher_provider_id" ).change(function() {
  var exist_id = $(this).val();
  var ids = $('#voucher_false_ids_')[0].value;
  if(jQuery.inArray(exist_id, ids.split(" ")) !== -1){
  $('label[for=voucher_name], input#voucher_name').hide();
  }
  else
  {
  $('label[for=voucher_name], input#voucher_name').show();
  }
  });
                                                                                                              

Then in /app/assets/javascript/mycode.js.coffee

jQuery ->
  $('label[for=voucher_name], input#voucher_name').hide();
  $( "#voucher_voucher_provider_id" ).change ->
    exist_id = $(this).val();
    ids = $('#voucher_false_ids_')[0].value;
    alert('alert');
    If(jQuery.inArray(exist_id, ids.split(" ")) !== -1)
      $('label[for=voucher_name], input#voucher_name').hide();
    else
      $('label[for=voucher_name], input#voucher_name').show();

So far, I able to run until .change -> alert('alert'); Not after I start put all line after If

which cause error:

ExecJS::RuntimeError at /admin
SyntaxError: [stdin]:6:51: unexpected =

Help: for proper syntax or what is the error coming from /Thanks

If you paste that snippet into the "Try CoffeScript" page of the CoffeeScript website, it tells you what's wrong.

It says this:

[stdin]:7:51: error: unexpected =
    If(jQuery.inArray(exist_id, ids.split(" ")) !== -1)
                                                  ^

CoffeeScript does not have === or !== operators. == and != are translated their strict equivalents when compiled for you.

If you fix that, you get another error:

[stdin]:8:1: error: unexpected indentation
      $('label[for=voucher_name], input#voucher_name').hide();
^^^^^^

This is because of the previous line. You start an if statement with If , so CoffeeScript does not see this as an if statement, but instead as a function call to the If() function.

Change that to lowercase if and then it compiles just fine.


Short version, here it is fixed:

jQuery ->
  $('label[for=voucher_name], input#voucher_name').hide();
  $( "#voucher_voucher_provider_id" ).change ->
    exist_id = $(this).val();
    ids = $('#voucher_false_ids_')[0].value;
    alert('alert');
    if(jQuery.inArray(exist_id, ids.split(" ")) != -1)
      $('label[for=voucher_name], input#voucher_name').hide();
    else
      $('label[for=voucher_name], input#voucher_name').show();

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