title: How can I access global javascript variables in unobtrusive javascript in a Rails app?
body: <p>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...</p>
  
  <pre><code>$(function() {
    var editor = ace.edit("editor");
  }):
  </code></pre>
  
  <p>In that view, I have a simple ajax button...</p>
  
  <pre><code>&lt;%= button_to "Set Content", {controller: :pages, action: 'set_content'}, remote: true %&gt;
  </code></pre>
  
  <p>that requests some unobtrusive javascript...</p>
  
  <pre><code>editor.setValue("New content has been set");
  </code></pre>
  
  <p>but this doesn't work. I'm guessing this doesnt work because <code>editor</code> 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.</p>
  
  <p>I was under the impression that if I write <code>var editor</code>, then is declared as a global variable that my unobtrusive javascript should be able to access.</p>
  
  <p>My questions are...</p>
  
  <ol>
  <li>How can I access global variables inside of my unobtrusive javascript code?</li>
  <li>Since global variables are considered evil, is there a better way to access the <code>editor</code> variable from my unobtrusive javascript?</li>
  </ol>
  
  <p>Thanks in advance for your wisdom!</p>