Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

javascript - 使用Rails 3.1,您在哪里放置“特定于页面”的JavaScript代码?(Using Rails 3.1, where do you put your “page specific” JavaScript code?)

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代码中,您只需检查页面上是否包含idclass ,如果是,则运行与其关联的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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Asset Pipeline docs suggest how to do controller-specific JS:(Asset Pipeline文档建议如何执行特定于控制器的JS:)

For example, if a ProjectsController is generated, there will be a new file at app/assets/javascripts/projects.js.coffee and another at app/assets/stylesheets/projects.css.scss .(例如,如果生成了ProjectsController ,则app/assets/javascripts/projects.js.coffee会有一个新文件, app/assets/stylesheets/projects.css.scss会有另一个文件。) You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these controllers with lines such as <%= javascript_include_tag params[:controller] %> or <%= stylesheet_link_tag params[:controller] %> .(您应该将任何JavaScript或CSS唯一的控件放在各自的资产文件中,因为这些文件可以仅为这些控制器加载,例如<%= javascript_include_tag params[:controller] %><%= stylesheet_link_tag params[:controller] %> 。) Link to: asset_pipeline(链接到:asset_pipeline)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...