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?
2 Answers 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.
Comments
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.
Comments
Explore related questions
See similar questions with these tags.
update_checkboxes), then you use 2