I’m a relative amateur at coffeescript/javascript, but I’m having trouble figuring out how to structure it so that three different functions (two Select2 functions and one function to load Best in Place) will work at the same time in a Rails 3 application. I have the following code in an asset pipeline file that’s definitely getting loaded and converted to javascript, when the app runs:
jQuery ->
$('#foo').select2()
placeholder: "Choose an option.",
allowClear: true
$('#bar').select2()
placeholder: "Choose an option.",
allowClear: true
$('.best_in_place').best_in_place()
But when I put all three functions together like this, none of them work. When I run just one, like:
jQuery ->
$('#foo').select2()
placeholder: "Choose a diagnosis.",
allowClear: true
Presto, that function works just fine. What am I doing wrong here?
I suspect that your code doesn’t really work at all, you might want to watch your JavaScript console when you try to run that code.
This:
is the same as this JavaScript:
but you almost certainly want this JavaScript:
To get that, your CoffeeScript would look like this:
or like this:
This CoffeeScript:
should successfully execute
$('#foo').select2()and then trigger a TypeError becauseselect2()isn’t returning a function but you’re trying to call it like one. Then, the$('#bar').select2()and$('.best_in_place').best_in_place()won’t even run.