So in app/assets/javascript/faye.js.coffee.erb
I have the following:
$('#room_tag').bind('blur', () ->
alert('Hey!')
)
All the other code in it such as: sendmessage('room', 'message')
work just fine. And I can copy and paste the code generated from the block above and paste it into Chrome it works fine. I assume this is because, is it rails or coffeescript?, either way one of them, wraps the entire file in:
(function() {
// your generated code here
}).call(this);
Also would there happen to be a way for me to access methods that are defined within there? Is it possible to define a method in there without assigning it to a variable?
1) Most likely your .bind
call is executing too soon, before the document is ready and thus it doesn't do anything. Wrap it in a call to $(document).ready
like this
$(document).ready ->
$('#room_tag').bind 'blur', ->
alert 'Hey!'
And there's actually a cute shortcut for this since jQuery's default $
function is an alias for $(document).ready
, you can just do:
$ ->
$('#room_tag').bind 'blur', ->
alert 'Hey!'
2) It is coffeescript that wraps everything in a self-executing function definition.
3) If you want to make a global function in coffeescript, explicitly assign it as a property of the global window object
window.myFunc = (arg1) ->
alert arg1
2) & 3) are clearly explained in the CoffeeScript docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With