简体   繁体   English

在 JavaScript 中使用 Ruby 变量

[英]Using Ruby Variables in JavaScript

I'm new to Ruby so I had a question in regards to using some variables throughout my codebase.我是 Ruby 的新手,所以我对在整个代码库中使用一些变量有疑问。 I have a variable declared in my Ruby file as follows:我在 Ruby 文件中声明了一个变量,如下所示:

@pages = 350

Now, I know that in HAML, I can simply do: -if @pages = 350 , and in HAML with inline javascript, I can do something like:现在,我知道在 HAML 中,我可以简单地执行以下操作: -if @pages = 350 ,在带有内联 javascript 的 HAML 中,我可以执行以下操作:

:javascript
   console.log("#{@pages}");

However, I am linking an external JavaScript file in my HAML document, and I was wondering if it would be possible to use my Ruby variables in my external JS document?但是,我在我的 HAML 文档中链接了一个外部 JavaScript 文件,我想知道是否可以在我的外部 JS 文档中使用我的 Ruby 变量? I need some variables in order to build what I am trying to build.我需要一些变量来构建我想要构建的东西。 Thanks!谢谢!

Update as per one of the answers:根据答案之一更新:

In my HAML file, I have the following:在我的 HAML 文件中,我有以下内容:

:javascript
   printPages(3);

In my external JS file, I have:在我的外部 JS 文件中,我有:

$(function() { 
   window.printPages = function(pages) { 
        console.log(pages);
    }
});

I you are loading static javascript files I really would not recommend trying to insert variables into that code.我正在加载 static javascript 文件我真的不建议尝试将变量插入该代码。

Instead, think about how you would provide that to your javascript code.相反,请考虑如何将其提供给 javascript 代码。


You could send it to that code as an argument.您可以将其作为参数发送到该代码。

<script type="text/javascript" src="./my-external-script.js"></script>

:javascript
  runMyExternalFunction(#{@pages})

Or you could add the variable as a data attribute on your page, and your javascript could look for that when it loads.或者您可以将变量添加为页面上的数据属性,并且您的 javascript 可以在加载时查找它。

Haml:哈姆尔:

%body data-pages=@pages

JS: JS:

console.log(document.body.dataset.pages) // value of @pages

Update about your javascript:更新您的 javascript:

Using jquery's on page ready event $() for declaring the function is a bad idea.使用 jquery 的页面就绪事件$()声明function 是一个坏主意。 That means that it waits for everything on the page to be compeletely loaded, and then declares the function your page needs.这意味着它等待页面上的所有内容完全加载,然后声明您的页面需要的 function。

What you should be doing is setting up all the function that will be needed right away, and then calling those functions when the page is loaded.您应该做的是立即设置所有需要的 function,然后在加载页面时调用这些函数。

They way you have it, the page loads, printPages() is executed (which doesn't exist yet), and then after all that printPages is created and put into the global scope.他们按照你的方式,加载页面,执行printPages() (它还不存在),然后在创建所有printPages并将其放入全局 scope 之后。

So remove the $() from your external script and add it to your HAML instead.因此,从您的外部脚本中删除$()并将其添加到您的 HAML 中。

In fact, if your aren't doing anything super fancy with transpiling or bundling, then you can simply declare the function in your external JS file, and it will be available globally.事实上,如果您没有在转译或捆绑方面做任何超级花哨的事情,那么您可以简单地在您的外部 JS 文件中声明 function,它将在全球范围内可用。 This doesn't work with your code because they way you've declared the function, it would only be available to code within that $() callback.这不适用于您的代码,因为它们以您声明 function 的方式,它只能用于该$()回调中的代码。

// js file
function printPages(pages) { 
    console.log(pages);
}

HAML:哈姆勒:

$(() => {
  printPages({@pages})
})

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

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