处理 Rails 3 应用程序中冲突的 javascript 库

发布于 2024-09-08 21:19:40 字数 559 浏览 2 评论 0原文

我想了解在 Rails 3 应用程序中使用多个(且有冲突的)JavaScript 库时的最佳方法。在引发 js 冲突之前,我使用 application.html.erb 模板在所有页面上定义所有 javascript 库模板:

<%= javascript_include_tag :all %>

我打算将所有 js 文件缓存到一个文件中。但是,当我使用 mootool js 库添加页面时,其他一些页面损坏了,并且我无法通过修改 js 库来解决问题。我找到了一个解决方案,为利用 mootool 库的页面创建一个新的控制器 - 然后我为该控制器创建了一个模板,该控制器不使用其他(非 mootool)页面使用的应用程序模板。

在处理冲突的库时,我是否需要创建一个新的控制器(带有定义 js 库的新 head 部分),或者这是一个更好的解决方案。

另外,我可以不再将所有库缓存到一个文件中吗?我想我可以创建一个包含所有不冲突(并缓存它们)的 javascript 库的部分,并链接到应用程序模板中的部分。在使用 mootools 的页面上,我将链接到该部分并声明 mootool js 库。

Id like to understand the best approach when using multiple (and conflicting) javascript libraries in a rails 3 app. Before the js conflict was raised, I used the application.html.erb template to define all javascript libraries template on all pages with:

<%= javascript_include_tag :all %>

I intended to cache all the js files into one file. However, when I added a page using the mootool js library, a few of my other pages broke and I couldn't resolve the issue by modifying the js libraries. I've found a solution by creating a new controller for the pages that utilize the mootool library - then I created a template for that controller that does not use the application template that the other (non-mootool) pages use.

When dealing with a conflicting library, do I need to create a new controller (with a new head section that defines the js libraries), or this there a better solution.

Also, can I no longer cache all libraries into one file? Im thinking I can create a partial with all the javascript libraries that don't conflict (and cache them) and link to the partial in the application template. And on the pages that use mootools, I'll link to that partial and also declare the mootool js library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2024-09-15 21:19:40

我通过挖掘老式的 RailsCast 其中 Ryan Bates 解释了如何为控制器中的一个操作选择布局。基本的东西。

更好的解决方案是解决冲突的 javascript 库,这样我就不必使用多个布局。

I was able to find part of the answer by digging up an old-school RailsCast where Ryan Bates explains how to pick a layout for only one action in a controller. Basic stuff.

The better solution is to resolve the conflicting javascript libraries so that I won't have to use multiple layouts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文