0

So I have this javascript That works by itself

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

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.

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?

asked Jan 28, 2015 at 14:22
2
  • 3
    maybe your inconsistent tabs? (4 spaces under update_checkboxes), then you use 2 Commented Jan 28, 2015 at 14:24
  • I would say your indention is off, this script looks good to me Commented Jan 28, 2015 at 14:30

2 Answers 2

2

Remember that in CoffeeScript, indentation matters, and you can't mix spaces and tabs reliably.

This CoffeeScript, in which indentation has been standardized to two spaces:

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:

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.

answered Jan 28, 2015 at 14:26
Sign up to request clarification or add additional context in comments.

Comments

1

Your indentation must be off. I pasted your exact coffeescript from the question into 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. Take a look at the original and fix the mismatched indentation.

answered Jan 28, 2015 at 14:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.