简体   繁体   English

从Javascript到Coffeescript

[英]Javascript to Coffeescript

So I have this javascript That works by itself 所以我有这个javascript本身可以工作

function update_checkboxes()
      {
        var part_array = [];
        $("input#edit:checked").parents('td').next().each(function(){
          part_id = $(this).text();
          part_array.push(part_id);
        });
        $("input#participant_ids").val(JSON.stringify(part_array));
      }

That I converted to coffeescript 我转换成咖啡脚本

update_checkboxes = ->
    part_array = []
    $("input#edit:checked").parents("td").next().each ->
      part_id = $(this).text()
      part_array.push part_id

    $("input#participant_ids").val JSON.stringify(part_array)

The problem is when I do rails rake assets:precompile , it compile fine but in Firebug I gives error 问题是当我做Rails rake assets:precompile ,它可以正常rake assets:precompile ,但是在Firebug中,我给出了错误

ReferenceError: part_array is not defined

$("input#participant_ids").val(JSON.stringify(part_array));

due that the part_array gets defined in a different block. 由于part_array在另一个块中定义。

update_checkboxes = function() {
    var part_array;
    return part_array = [];
  };

  $("input#edit:checked").parents("td").next().each(function() {
    var part_id;
    part_id = $(this).text();
    return part_array.push(part_id);
  });

  $("input#participant_ids").val(JSON.stringify(part_array));

}).call(this); 

What I'm doing wrong here? 我在这里做错了什么?

Remember that in CoffeeScript, indentation matters , and you can't mix spaces and tabs reliably. 请记住,在CoffeeScript中,缩进很重要 ,并且您不能可靠地混合使用空格和制表符。

This CoffeeScript, in which indentation has been standardized to two spaces: 此CoffeeScript中的缩进已标准化为两个空格:

update_checkboxes = ->
  part_array = []
  $("input#edit:checked").parents("td").next().each ->
    part_id = $(this).text()
    part_array.push part_id

  $("input#participant_ids").val JSON.stringify(part_array)

Results in this JavaScript: 此JavaScript中的结果:

var update_checkboxes;
update_checkboxes = function() {
  var part_array;
  part_array = [];
  $("input#edit:checked").parents("td").next().each(function() {
    var part_id;
    part_id = $(this).text();
    return part_array.push(part_id);
  });
  return $("input#participant_ids").val(JSON.stringify(part_array));
};

...which doesn't end up with part_array defined in a separate block. ...不会以在单独的块中定义的part_array结尾。

Your indentation must be off. 您的缩进必须没有。 I pasted your exact coffeescript from the question into js2coffee 我将问题中的确切 coffeescript粘贴到js2coffee中

update_checkboxes = ->
    part_array = []
    $("input#edit:checked").parents("td").next().each ->
      part_id = $(this).text()
      part_array.push part_id

    $("input#participant_ids").val JSON.stringify(part_array)

and received... 并收到...

var update_checkboxes;

update_checkboxes = function() {
  var part_array;
  part_array = [];
  $("input#edit:checked").parents("td").next().each(function() {
    var part_id;
    part_id = $(this).text();
    return part_array.push(part_id);
  });
  return $("input#participant_ids").val(JSON.stringify(part_array));
};

Which is correct. 哪个是对的。 StackOverflow must have normalized your indentation when you pasted it. 粘贴时,StackOverflow必须已将您的缩进标准化。 Take a look at the original and fix the mismatched indentation. 查看原件并修复不匹配的缩进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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