使用Rails 3.1,你在哪里放置你的“特定页面” JavaScript代码? [英] Using Rails 3.1, where do you put your "page specific" JavaScript code?

查看:143
本文介绍了使用Rails 3.1,你在哪里放置你的“特定页面” JavaScript代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,您的所有JavaScript都合并为1个文件。 Rails在默认情况下将 // = require_tree。添加到 application.js 清单文件的底部。



这听起来像是一个真正的生命保护程序,但我有点担心特定于页面的JavaScript代码。此代码是否在每个页面上执行?我想要的最后一件事是,只需要在1页上需要为每个页面实例化我的所有对象。



此外,是否有可能发生冲突的代码?



或者你是否放了一个<页面底部的code> script 标签只是调用执行页面的javascript代码的方法?



你不再需要require.js吗?



谢谢



编辑:我很欣赏所有答案......我认为他们并没有真正解决问题。其中一些是关于造型而且似乎没有关联...而其他人只是提到 javascript_include_tag ...我知道存在(显然......)但它会看来Rails 3.1的方法是将所有JavaScript包装成1个文件,而不是在每个页面的底部加载单独的JavaScript。



最好的解决方案我可以想出的是用 id s或 class <包装 div 标签中的某些功能/ code> ES。在JavaScript代码中,您只需检查页面上是否有 id ,如果是,则为运行与之关联的JavaScript代码。这样,如果动态元素不在页面上,则JavaScript代码不会运行 - 即使它已包含在由Sprockets打包的大量 application.js 文件中。 / p>

我的上述解决方案的好处是,如果在100个页面中的8个页面中包含搜索框,则它将仅在这8个页面上运行。您也不必在网站的8个页面上包含相同的代码。事实上,您永远不必在任何地方再次在您的网站上添加手动脚本标记。



我认为这是我问题的实际答案。

解决方案

我很欣赏所有的答案......而且我认为他们并没有真正解决这个问题。其中一些是关于造型而且似乎没有关联...而其他人只是提到 javascript_include_tag ...我知道存在(显然......)但它会看来Rails 3.1的方法是将你所有的Javascript包装成1个文件,而不是在每个页面的底部加载单独的Javascript。



最好的解决方案我可以想出的是用 id s或 class <包装 div 标签中的某些功能/ code> ES。在javascript代码中。然后你只需检查页面上是否有 id ,如果是,则运行javascript代码与之相关的。这样,如果动态元素不在页面上,则javascript代码不会运行 - 即使它已包含在由Sprockets打包的大量 application.js 文件中。 / p>

我的上述解决方案的好处是,如果在100个页面中的8个页面中包含搜索框,则它将仅在这8个页面上运行。您也不必在网站的8个页面上包含相同的代码。事实上,你永远不必再在你的网站上包含手动脚本标签 - 除了可能预加载数据。



我认为这是我的实际答案问题。


To my understanding, all of your JavaScript gets merged into 1 file. Rails does this by default when it adds //= require_tree . to the bottom of your application.js manifest file.

This sounds like a real life-saver, but I am a little concerned about page-specific JavaScript code. 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.

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?

Do you no longer need require.js then?

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.

The best solution I can come up with is to wrap certain features in div tags with ids or classes. 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. 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.

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. You also won't have to include the same code on 8 of the pages on the site. 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.

解决方案

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.

The best solution I can come up with is to wrap certain features in div tags with ids or classes. In the javascript code. Then 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. 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.

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. You also won't have to include the same code on 8 of the pages on the site. In fact, you'll never have to include manual script tags on your site anywhere ever again - except to maybe preload data.

I think this is the actual answer to my question.

这篇关于使用Rails 3.1,你在哪里放置你的“特定页面” JavaScript代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆