简体   繁体   English

如何将JavaScript全局变量传递给requirejs,Ruby on Rails

[英]How to pass a javascript global variable into requirejs, ruby on rails

I am implemented Recurly payments into a project, the project is using requirejs and there are requirejs 2 javascript files, 1 named 'front.js.coffee' for customer facing site and the second named 'admin.js.coffee' for the CMS. 我已将Recurly Payment实施到一个项目中,该项目正在使用requirejs,并且有requirejs 2个javascript文件,其中一个面向客户站点,一个名为“ front.js.coffee”,另一个用于CMS,名为“ admin.js.coffee”。 I have created a global variable as a partial that I render into the website header and then I need the front.js file to pull this value in order to build the Recurly transaction form. 我创建了一个全局变量作为局部变量,并将其渲染到网站标题中,然后需要front.js文件提取此值,以构建Recurly事务表单。 I had to do it this way because I need the server to generate a signature so therefore this had to pass through a '*html.erb' file. 我必须这样做,因为我需要服务器生成签名,因此必须通过“ * html.erb”文件进行传递。

So here is the global variable inside a shared view file: 因此,这是共享视图文件中的全局变量:

<%= javascript_tag do %>
   signature_value: '<%= j RECURLY_SIGNATURE %>'
<% end %> 

So here is the JS inside the front.js file: 所以这是front.js文件中的JS:

Recurly.config
 subdomain: 'iewebinar'
 currency: 'USD'
 country: 'US'
Recurly.buildTransactionForm
 target: '#recurly-transaction'
 successURL: '/webinars/thank_you.html'
 signature: 'signature_value'

I am new to Ruby on Rails development and I dont entirely understand Requirejs so simple instructions would be great :) 我是Ruby on Rails开发的新手,并且我不完全了解Requirejs,因此简单的说明非常有用:)

before you requiring the js files, you can add 在需要js文件之前,您可以添加

<script>
  window.signature_value = '<%= j RECURLY_SIGNATURE %>'
</script>

then just use window.signature_value in your config. 然后只需在您的配置中使用window.signature_value

successURL: '/webinars/thank_you.html'
signature: window.signature_value

one more option is to change the filename to admin.js.coffee.erb . 另一种选择是将文件名更改为admin.js.coffee.erb This will give you access to ruby functions so you may have access to the constant (sorry i'm not sure about this). 这将使您可以访问ruby函数,因此 可以 访问常量 (对不起,我对此不确定)。

successURL: '/webinars/thank_you.html'
signature: '<%= RECURLY_SIGNATURE %>'

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

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