To my understanding, all of your JavaScript gets merged into 1 file.(据我所知,您的所有JavaScript都合并为1个文件。)
Rails does this by default when it adds //= require_tree .
(Rails在添加//= require_tree .
时默认执行此操作//= require_tree .
) to the bottom of your application.js
manifest file.(到application.js
清单文件的底部。)
This sounds like a real life-saver, but I am a little concerned about page-specific JavaScript code.(这听起来像是一个真正的生命保护程序,但我有点担心特定于页面的JavaScript代码。) Does this code get executed on every page?(此代码是否在每个页面上执行?) The last thing I want is for all of my objects to be instantiated for every page when they are only needed on 1 page.(我想要的最后一件事是,只需要在1页上需要为每个页面实例化我的所有对象。)
Also, isn't there potential for code that clashes too?(此外,代码是否也存在冲突的可能性?)
Or do you put a small script
tag at the bottom of the page that just calls into a method that executes the javascript code for the page?(或者你是否在页面底部放了一个小script
标签,只是调用一个执行页面javascript代码的方法?)
Do you no longer need require.js then?(那你不再需要require.js了吗?)
Thanks(谢谢)
EDIT : I appreciate all the answers... and I don't think they are really getting at the problem.(编辑 :我很感谢所有的答案......我认为他们并没有真正解决问题。) Some of them are about styling and don't seem to relate... and others just mention javascript_include_tag
... which I know exists (obviously...) but it would appear that the Rails 3.1 way going forward is to wrap up all of your JavaScript into 1 file rather than loading individual JavaScript at the bottom of each page.(其中一些是关于样式的,似乎没有关联...而其他人只是提到javascript_include_tag
...我知道存在(显然......)但是看起来Rails 3.1的方式将会结束所有将您的JavaScript分成1个文件,而不是在每个页面底部加载单独的JavaScript。)
The best solution I can come up with is to wrap certain features in div
tags with id
s or class
es.(我能拿出最好的解决办法是包装在某些功能div
标签与id
S或class
上课。) In the JavaScript code, you just check if the id
or class
is on the page, and if it is, you run the JavaScript code that is associated with it.(在JavaScript代码中,您只需检查页面上是否包含id
或class
,如果是,则运行与其关联的JavaScript代码。) This way if the dynamic element is not on the page, the JavaScript code doesn't run - even though it's been included in the massive application.js
file packaged by Sprockets.(这样,如果动态元素不在页面上,则JavaScript代码不会运行 - 即使它已包含在由Sprockets打包的大量application.js
文件中。)
My above solution has the benefit that if a search box is included on 8 of the 100 pages, it will run on only those 8 pages.(我的上述解决方案的好处是,如果在100个页面中的8个页面中包含搜索框,则它将仅在这8个页面上运行。) You also won't have to include the same code on 8 of the pages on the site.(您也不必在网站的8个页面上包含相同的代码。) In fact, you'll never have to include manual script tags on your site anywhere ever again.(实际上,您永远不必在任何地方再次在您的网站上包含手动脚本标记。)
I think this is the actual answer to my question.(我认为这是我问题的实际答案。)
ask by Fire Emblem translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…