One of the views in my Rails 4 app loads a simple javascript to create an instance of the Ace editor once the page loads...
$(function() {
var editor = ace.edit("editor");
}):
In that view, I have a simple ajax button...
<%= button_to "Set Content", {controller: :pages, action: 'set_content'}, remote: true %>
that requests some unobtrusive javascript...
editor.setValue("New content has been set");
but this doesn't work. I'm guessing this doesnt work because editor isn't defined. Since ajax calls fail silently and I don't know how to debug unobtrusive javascript code using the same Chrome tools that I use debug normal javascript code, I can't verify that's the actual problem, but that's my best guess.
I was under the impression that if I write var editor, then is declared as a global variable that my unobtrusive javascript should be able to access.
My questions are...
- How can I access global variables inside of my unobtrusive javascript code?
- Since global variables are considered evil, is there a better way to access the
editorvariable from my unobtrusive javascript?
Thanks in advance for your wisdom!