简体   繁体   English

如何在Rails应用程序中使用不显眼的javascript访问全局javascript变量?

[英]How can I access global javascript variables in unobtrusive javascript in a Rails app?

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... 页面加载后,Rails 4应用程序中的一个视图将加载一个简单的javascript以创建Ace编辑器的实例...

$(function() {
  var editor = ace.edit("editor");
}):

In that view, I have a simple ajax button... 在这种情况下,我有一个简单的ajax按钮...

<%= button_to "Set Content", {controller: :pages, action: 'set_content'}, remote: true %>

that requests some unobtrusive javascript... 要求一些不引人注目的JavaScript ...

editor.setValue("New content has been set");

but this doesn't work. 但这不起作用。 I'm guessing this doesnt work because editor isn't defined. 我猜这是行不通的,因为未定义editor 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. 由于ajax调用会以静默方式失败,并且我不知道如何使用与调试普通javascript代码相同的Chrome工具来调试不引人注意的javascript代码,因此无法验证这是否是实际问题,但这是我的最佳猜测。

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. 我的印象是,如果我编写var editor ,则将其声明为全局变量,我的Java语言应该可以访问它。

My questions are... 我的问题是...

  1. How can I access global variables inside of my unobtrusive javascript code? 如何在不引人注目的JavaScript代码内部访问全局变量?
  2. Since global variables are considered evil, is there a better way to access the editor variable from my unobtrusive javascript? 由于全局变量被认为是邪恶的,是否有更好的方法从我的入门javascript中访问editor变量?

Thanks in advance for your wisdom! 在此先感谢您的智慧!

when you use the var keyword inside a function, the scope of that variable is local to the function, so you are incorrect in your assumption about global scope. 当在函数内部使用var关键字时,该变量的作用域是该函数的局部作用,因此您对全局作用域的假设不正确。 To make the variable global, declare it outside of your function: 要使变量成为全局变量,请在函数外部声明它:

var editor;
$(function(){
   editor = ace.edit('editor');
});

alternatively you could simply reference the window object (which is the 'global' object in browsers 或者,您可以简单地引用window对象(这是浏览器中的“全局”对象

$(function(){
   window.editor = ace.edit('editor');
});

With regards to avoiding global variables, it is reasonable to use a single global variable (often called 'app', or 'myCompany' or such to provide a namespace that is globally accessible. 关于避免全局变量,使用单个全局变量(通常称为“ app”或“ myCompany”等)来提供可全局访问的名称空间是合理的。

var app = {};
$(function(){
  app.editor = ace.edit('editor');
});

This at least limits the scope of your new variables so you don't mistakenly overwrite global variables. 这至少限制了新变量的范围,因此您不会错误地覆盖全局变量。 However the real answer to avoiding global variables lies in the overall architecture of your app, which can't really be addressed without seeing more code. 但是,避免全局变量的真正答案在于应用程序的整体体系结构,如果不查看更多代码,就无法真正解决该问题。 Generally speaking if ALL of the code you write is inside your jquery ready function, then you don't need global variables. 一般来说,如果您编写的所有代码都在jquery ready函数内部,则不需要全局变量。 Frameworks such as angular, ember, and backbone provide a much better structure for this type of thing than the ad-hoc code common with simple uses of jquery. 与简单使用jquery时常见的即席代码相比,诸如angular,ember和骨干之类的框架为此类事物提供了更好的结构。

$(function() {
  var editor = ace.edit("editor");
});

creates a local variable editor within the context of it's callback function. 在其回调函数的上下文中创建一个局部变量编辑器。 To access it elsewhere, you'll need to make it global window.editor = ace.edit("editor") , or add it to a namespace with something like 要在其他位置访问它,您需要将其设置为全局window.editor = ace.edit("editor") ,或使用类似以下内容将其添加到命名空间中

window.App = {};
App.editor = ace.edit("editor");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM