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

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

问题描述

据我所知,您的所有 JavaScript 都合并到 1 个文件中.默认情况下,Rails 在将 //= require_tree . 添加到 application.js 清单文件的底部时会执行此操作.

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.

这听起来像是真正的救星,但我有点担心特定于页面的 JavaScript 代码.这段代码会在每一页上执行吗?我想要的最后一件事是当我的所有对象只在一页上需要时,为每个页面实例化.

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?

或者你是否在页面底部放置一个小的 script 标签,它只是调用一个方法来执行页面的 javascript 代码?

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?

那你不再需要 require.js 了吗?

Do you no longer need require.js then?

谢谢

编辑:我很感激所有的答案……而且我认为他们并没有真正解决问题.其中一些是关于样式的,似乎并不相关......而其他人只是提到 javascript_include_tag......我知道它存在(显然......)但它似乎是 Rails 3.1 方式下一步是将所有 JavaScript 打包到 1 个文件中,而不是在每个页面底部加载单独的 JavaScript.

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.

我能想到的最佳解决方案是用 ids 或 classes 将某些功能包装在 div 标签中.在 JavaScript 代码中,您只需检查 idclass 是否在页面上,如果是,则运行与其关联的 JavaScript 代码.这样,如果页面上没有动态元素,JavaScript 代码就不会运行——即使它包含在 Sprockets 打包的大量 application.js 文件中.

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.

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

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.

推荐答案

我很感激所有的答案......而且我认为他们并没有真正解决问题.其中一些是关于样式的,似乎并不相关......而其他人只是提到 javascript_include_tag......我知道它存在(显然......)但它似乎是 Rails 3.1 的方式下一步是将所有 Javascript 打包到 1 个文件中,而不是在每个页面的底部加载单独的 Javascript.

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.

我能想到的最佳解决方案是用 ids 或 classes 将某些功能包装在 div 标签中.在javascript代码中.然后您只需检查 idclass 是否在页面上,如果是,则运行与之关联的 javascript 代码.这样,如果页面上没有动态元素,javascript 代码就不会运行 - 即使它已包含在由 Sprockets 打包的大量 application.js 文件中.

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.

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

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天全站免登陆