Rails 4 jQuery conflicts with javascript

Rails 4 jQuery conflicts with javascript

本文关键字:with javascript conflicts jQuery Rails      更新时间:2023-09-26

我试图在Rails 4中制作一个应用程序。

我在挣扎。我正在尝试合并引导主题,但供应商 javascript 和其他代码遇到了问题。

我认为问题可能与在我的应用程序中使用 jQuery 有关.js然后让供应商.js以"$"号开头的文件:

$.circleProgress = {

我刚刚读到这个:https://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/

我的问题是我不知道如何进行更改以使代码安全。

我是否需要搜索供应商文件中的每个"$",或者如何在 jQuery 上放置安全包装器 - 尤其是在通过 gem 合并时。

有没有人遇到过这些问题并找到了解决方案?我希望得到一些帮助。

应用.js

//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require html.sortable
//= require disqus_rails
//= require moment
//= require bootstrap-datetimepicker
//= require pickers
//= require main
//= require hammer.min
//= require jquery.animate-enhanced.min
//= require jquery.countTo
//= require jquery.easing.1.3
//= require jquery.fitvids
//= require jquery.magnific-popup.min
//= require jquery.parallax-1.1.3
//= require jquery.properload
//= require jquery.shuffle.modernizr.min
//= require jquery.sudoSlider.min
//= require jquery.superslides.min
//= require masonry.pkgd.min
//= require rotaterator
//= require smoothscrolljs
//= require waypoints.min
//= require wow.min
//= require gmaps/google
//= require chosen-jquery
//= require cocoon
//= require imagesloaded.pkgd.min
//= require isotope.pkgd.min
//= require jquery.counterup.min
//= require jquery.pjax
//= require custom.js
//= require slider
//= require underscore
//= require dependent-fields
//= require_tree .
//= require bootstrap-slider

$(document).ready(function() {
    DependentFields.bind()
});

相关问题:Rails 4 - 整合供应商资产

观察

我注意到控制台错误日志显示我的应用程序/资产/javascripts 文件夹中的.js文件存在问题。

它们是该文件夹中唯一的其他文件(除了应用程序.js)。我的应用程序.js有"require_tree",因此它们将被编译。但是我认为这些文件中的某些内容可能与(也许)在应用程序中包含jQuery.js冲突?

日志显示:

Uncaught SyntaxError: Identifier 'findGoodContent' has already been declared
circle-progress.self-f67ec54c54a06da27d11cda862036a058792eadc8ef982df2e7c0a1682536c31.js:9 Uncaught TypeError: Cannot set property 'circleProgress' of undefined
conversations.self-832ece2867c1701a5202459ab73ecd6432da2a6c833d8822d37025845a0aefcc.js:10 Uncaught TypeError: $ is not a function
organisations.self-6547f734e3a69b76176dfe5bb194e428c0bc560ad3fb69811dce9c7f8ccb9f4d.js:2 Uncaught TypeError: $ is not a function
http://localhost:3000/assets/%3C%=%20asset_path('random.jpg')%20%%3E Failed to load resource: the server responded with a status of 400 (Bad Request)
http://localhost:3000/profiles/assets/images/grayscale.svg#greyscale Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:3000/assets/vendor/assets/fonts/Simple-Line-Icons.woff Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:3000/profiles/assets/custom/images/preloader.gif Failed to load resource: the server responded with a status of 404 (Not Found)
chrome-extension://mkjojgglmmcghgaiknnpgjgldgaocjfd/content/contentScripts/kwift.CHROME.min.js:1384 Uncaught SyntaxError: Identifier 'findGoodContent' has already been declared
http://localhost:3000/assets/flaticon.woff Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:3000/assets/vendor/assets/fonts/Simple-Line-Icons.ttf Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:3000/assets/flaticon.ttf Failed to load resource: the server responded with a status of 404 (Not Found)
main.self-5888479bd3eb03114ce5776dd32cfadf84f1d3a4335043513f8b1606d3ab5f4a.js:316 Uncaught TypeError: dp(...).multipleFilterMasonry is not a function

每个被标识为错误的 js 文件夹都保存为应用程序/资产/javascript 中的.js文件。

我是否需要使用不同的文件名(.js.erb/js.coffee)?或者,我是否需要在这些文件的内容周围放置脚本标记?

例如,第一个文件打开时显示:

$.circleProgress = {
    // Default options (you may override them)
    defaults: {
        /**
         * This is the only required option. It should be from 0.0 to 1.0
         * @type {float}
         */
        value: 0,

更新

我认为我的问题的一部分与保存在带有".js"扩展名的应用程序/资产/javascript 中的文件有关。它们中的每一个在控制台错误列表中显示为:

kwift.CHROME.min.js:1271Uncaught SyntaxError: Identifier 'findGoodContent' has already been declared
circle-progress.self-f67ec54….js?body=1:9 Uncaught TypeError: Cannot set property 'circleProgress' of undefined(anonymous function) @ circle-progress.self-f67ec54….js?body=1:9
conversations.self-832ece2….js?body=1:10 Uncaught TypeError: $ is not a function(anonymous function) @ conversations.self-832ece2….js?body=1:10
organisations.self-6547f73….js?body=1:2 Uncaught TypeError: $ is not a function(anonymous function) @ organisations.self-6547f73….js?body=1:2(anonymous function) @ organisations.self-6547f73….js?body=1:6
projects.self-9c019ba….js?body=1:1 Uncaught TypeError: $ is not a function(anonymous function) @ projects.self-9c019ba….js?body=1:1
main.self-5888479….js?body=1:316 Uncaught TypeError: dp(...).multipleFilterMasonry is not a functionwindow.onload @ main.self-5888479….js?body=1:316
kwift.CHROME.min.js:1271 Uncaught SyntaxError: Identifier 'findGoodContent' has already been declared

听起来你有很多清理工作要做。这是你的第一个Rails应用程序吗?如果是这样,我很好奇为什么你已经有这么多 JS 文件。我会把你的清单减少到几个,解决剩下的问题,慢慢地开始添加东西,边走边解决问题。这将是我的总体战略。对于每个错误,请放慢速度并仔细检查您是否真正了解原因,然后再在这里和那里斜杠希望修复。

以下是我看到的特定错误的一些提示:

此行将包括app/assets/javascripts中的所有.js文件:

//= require_tree .

问题是你已经在上面一一包含它们了。所以他们被包括了两次。这就是为什么您会收到像 'findGoodContent' has already been declared .我知道使用require_tree很好(尽管我不喜欢它),所以如果你想保留它,请考虑将所有 3rd 方 JS 文件移出app/assets/javascripts并移入vendor/assets/javascripts。您仍然可以在application.jsrequire它们,但require_tree不会重复它们。

grayscale.svgflaticon.woff以及其他图像/字体资产等内容的 404 错误是因为您在 app/assets(或vendor/assets)中没有这些文件。或者,如果你这样做了,你应该确保你的JS/CSS文件和视图文件引用它们<%= asset_path "grayscale.svg" %>这样当Rails破坏它们的路径时,你仍然从正确的位置加载它们。阅读有关image_url和朋友的信息,并确保您了解它们的工作原理。默认情况下,资源管线在开发中提供未修改的名称,在生产中提供未修改的名称,因此在您将其全部推送之前,事情可能会正常工作!

此外,如果您在CSS文件或JS文件中包含<%= foo %>标签,则需要向该文件添加.erb扩展名,以便Rails知道处理这些标签。这就是为什么您在尝试加载此内容时收到 404 错误的原因:

http://localhost:3000/assets/%3C%=%20asset_path('random.jpg')%20%%3E

在您的一个文件(可能是 CSS 文件)中,您正在执行如下操作:

background-image: url("<%= asset_path('random.jpg') %>");

但是该文件需要一个.erb扩展名:blah.css.erbblah.scss.erb或其他什么。

$ is not a function这样的错误意味着这些文件依赖于jQuery,但jQuery $对象没有定义。如果你使用的是noConflict,正如有人在评论中建议的那样,你应该停止,因为你确实希望$成为jQuery对象。

祝你好运!如果您感到不知所措,我真的会从尽可能多地削减 JS require行开始,然后一次解决一个错误。

此外,我会对require行重新排序,以便依赖项位于顶部(例如 jquery),而依赖它们的东西较低。

哦,还有一件事,不是真正的错误:我会避免将已经缩小的文件(如hammer.min.js)放入您的存储库。资源管线知道如何缩小。因此,请检查未缩小的版本,以便如果您必须阅读源代码,则可以。

我把这篇文章作为一份关于工作的工作。

解决方案很复杂,但如果它可以帮助其他人了解出了什么问题:

  1. 有些 gem 需要 jQuery 功能。这可能与应用程序中通常包含的jQuery相冲突.js - 我编写了一些特定的代码来管理特定的冲突。

  2. 在 application.js 中,您需要按顺序列出所有需求,以便依赖项位于依赖它们的 js 文件下方。很难知道哪个文件取决于什么。我永远不会一个人弄清楚这一点

  3. 我的 Rake 资产:预编译调用将已编译的资源放在公用文件夹中。我覆盖了它,因为它导致与其他调用发生冲突(尤其是在更新文件时)。

对于任何试图学习的人来说,这个答案都没有真正的帮助。我自己并没有完全学会如何解决这些问题,但我确实找到了一位工作专家,他能够提供帮助,以便我可以继续前进。

恐怕这是博客教程无法解决的混乱问题之一。

不知道为什么没有人发布这个,但是jQuery中的$.something只是jQuery.something的简写。

禁用此功能的功能已融入jQuery中

https://api.jquery.com/jquery.noconflict/

这是他们文档中的一个例子

jQuery.noConflict();
// Do something with jQuery
jQuery( "div p" ).hide();
// Do something with another library's $()
$( "content" ).style.display = "none";

从本质上讲,在手卷代码中使用"jQuery"与"$"比更改继承的库要容易得多。话虽如此,您应该向有问题的库提交一个问题来修复此功能,类似于jQuery的工作方式,因为jQuery非常流行。

祝你好运!