如何在 Rails 3.1 资产管道中分离 javascript 库和调用
我正在尝试掌握整个资产管道的窍门,并阅读有关它们的指南和一些教程。但有一点不太清楚,我是否应该将我的 javascript 资源文件视为库或放置实际运行的代码的位置,即 $(document).ready。因为默认情况下所有的 javascript 文件都包含在内,并且其中有多个 $(document).ready 会很奇怪,更不用说您不希望每个页面都运行 $(document).ready 函数首先。该怎么做呢?使用我的资产文件作为库并将实际调用放入我的视图中(丑陋)?或者有更好的方法来做到这一点吗?
I'm trying to get the hang of the whole asset pipeline thing, and read the guide and several tutorials about them. But one thing that doesn't become quite clear is wether I should view my javascript asset files as a library or a place to put code that is actually run i.e. $(document).ready. Because by default all the javascript files are included, and it would be weird to have several $(document).ready's in there, not to mention that you don't want the $(document).ready function for every page to be run in the first place. What would be the way to go at this? Use my asset files as a library and put actual calls in my views (ugly)? Or is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也遇到了这个问题。在大型项目中,您可以让某人将代码放入准备好的文档中,例如,为具有 containerdiv 内的每个 li 添加点击功能强>。
现在我们都可以说上面的代码太通用了,当然可能会影响应用程序其他部分的 li 标签,但是项目越大,你就越有可能遇到像这样的冲突会导致意想不到的行为。
我对为每个加载的页面运行一大堆文档就绪函数感到不舒服。我的解决方案不一定是完美的,但它是我已经采用并将与您分享的解决方案。
在每个页面的正文标记中,我添加了表示控制器和操作的数据元素。然后,我有一个文档就绪脚本,该脚本查找以控制器命名的类,并附加名称 Ready,例如 HomeReady。然后它将调用该类上以该操作命名的方法(假设它存在)。因此,在您的资产咖啡文件中,您可以编写:
这允许将控制权控制到操作级别。当我遇到你的问题时,我决定将这个解决方案打包成一个 gem,因为我已经在几个项目中使用了它。您可以在以下位置找到它:https://github.com/intrica/rails_document_ready
I too ran into this issue. In a large project you can have somebody put code into document ready to, for example, add a click function to each li within a div with class container.
Now we could all argue that the above code would be too generic and of course may affect li tags in other parts of the application, but the bigger the project, the more likely it is that you will run into a conflict like this leading to unexpected behaviour.
I for one am uncomfortable with a whole bunch of document ready functions running for each and every page loaded. My solution is not necessarily the perfect one, but it's one that I have taken up and will share with you.
In the body tag of each page I add data elements signifying the controller and the action. I then have one document ready script that looks for a class named after the controller with the name Ready appended e.g. HomeReady. It will then call a method on this class (presuming it exists) named after the action. So in your asset coffee file you could write:
This allows control right down to the action level. When I came across your question I decided to package this solution into a gem as I have already used it in several projects. You can find it at: https://github.com/intrica/rails_document_ready
如果您绝对不希望运行某段初始化代码,除非当前页面是特定的控制器/操作,那么您可以尝试在页面上添加一个空元素,并使用从该信息(如“posts_index”)构建的 id 使用这两个助手:
"#{controller_name}_#{action_name}"
然后在您的 javascript 中,您可以将代码包装在 if 语句中,该语句检查是否存在具有适当 id 的元素。
编辑:这是我在评论中提到的 js 部分的示例。
show.html.haml
= render 'map'
map.html.erb
(我通常使用 haml,但在 erb 中编写 js 更容易)它可能没有那么干净,并且它没有获得成为资产管道一部分的好处,但我不介意,因为它只是获得的东西包含在特定页面上。
If you absolutely don't want a certain piece of initialization code to be run unless the current page is a specific controller/action, then you can try adding an empty element on the page with an id built from that info like "posts_index" using these two helpers:
"#{controller_name}_#{action_name}"
Then in your javascript you can wrap the code inside an if statement that checks for the existence of an element with the appropriate id.
edit: Here's an example of the js partial that I mentioned in the comments.
show.html.haml
= render 'map'
map.html.erb
(I normally use haml but it's easier to write js in erb)It's probably not as clean as it could be and it doesn't get the benefits of being part of the asset pipeline but I don't mind because it's only something that gets included on a specific page.