本文翻译自: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代码中,您只需检查页面上是否包含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. 我认为这是我问题的实际答案。
参考:https://stackoom.com/question/PsWj/使用Rails-您在哪里放置-特定于页面-的JavaScript代码
You can add this line in your layout file (eg application.html.erb) to automatically load the controller specific javascript file (the one that was created when you generated the controller): 您可以在布局文件中添加此行(例如application.html.erb)以自动加载控制器特定的javascript文件(生成控制器时创建的文件):
<%= javascript_include_tag params[:controller] %>
You also could add a line to automatically load a script file in a per-action basis. 您还可以添加一行以自动加载脚本文件。
<%= javascript_include_tag params[:controller] + "/" + params[:action] %>
Just put your page scripts into a subdirectoriy named after the controller name. 只需将页面脚本放入以控制器名称命名的子目录中即可。 In these files you could include other scripts using =require. 在这些文件中,您可以使用= require包含其他脚本。 It would be nice to create a helper to include the file only if it exists, to avoid a 404 fail in the browser. 如果文件存在,创建一个帮助程序来包含文件会很好,以避免浏览器中的404失败。
I have another solution, which although primitive works fine for me and doesn't need any fancy selective loading strategies. 我有另一种解决方案,虽然原始对我来说很好,并且不需要任何花哨的选择性加载策略。 Put in your nornal document ready function, but then test the current windows location to see if it is the page your javascript is intended for: 放入你的nornal文档就绪函数,然后测试当前的windows位置,看看它是你的javascript用于的页面:
$(document).ready(function() {
if(window.location.pathname.indexOf('/yourpage') != -1) {
// the javascript you want to execute
}
}
This still allows all the js to be loaded by rails 3.x in one small package, but does not generate much overhead or any conflicts with pages for which the js isn't intended. 这仍然允许所有js由rails 3.x在一个小包中加载,但是不会产生很多开销或与js不打算的页面有任何冲突。
I agree with your answer, to check if that selector is there, use: 我同意你的回答,检查选择器是否在那里,使用:
if ($(selector).length) {
// Put the function that does not need to be executed every page
}
(didn't see anyone add the actual solution) (没有看到有人添加实际的解决方案)
您还可以将js分组到文件夹中,并继续使用资产管道根据页面选择性地加载您的JavaScript 。
This has been answered and accepted long ago, but I came up with my own solution based on some of these answers and my experience with Rails 3+. 这已经很久以前得到了回答和接受,但我根据其中的一些答案以及我对Rails 3+的经验提出了自己的解决方案。
The asset pipeline is sweet. 资产管道很好。 Use it. 用它。
First, in your application.js
file, remove //= require_tree.
首先,在application.js
文件中,删除//= require_tree.
Then in your application_controller.rb
create a helper method: 然后在application_controller.rb
创建一个辅助方法:
helper_method :javascript_include_view_js //Or something similar
def javascript_include_view_js
if FileTest.exists? "app/assets/javascripts/"+params[:controller]+"/"+params[:action]+".js.erb"
return '<script src="/assets/'+params[:controller]+'/'+params[:action]+'.js.erb" type="text/javascript"></script>'
end
end
Then in your application.html.erb
layout file, add your new helper among the existing javascript includes, prefixed with the raw
helper: 然后在您的application.html.erb
布局文件中,在现有的javascript包含中添加新的帮助application.html.erb
,前缀为raw
帮助程序:
<head>
<title>Your Application</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= raw javascript_include_view_js %>
</head>
Voila, now you can easily create view-specific javascript using the same file structure you use everywhere else in rails. 瞧,现在你可以使用你在rails中的其他地方使用的相同文件结构轻松创建特定于视图的javascript。 Simply stick your files in app/assets/:namespace/:controller/action.js.erb
! 只需将文件粘贴到app/assets/:namespace/:controller/action.js.erb
!
Hope that helps someone else! 希望能帮助别人!