如何使用Rails默认值和bower指定jQuery版本?

时间:2016-06-27 10:20:52

标签: jquery ruby-on-rails bower jquery-rails

我的rails4应用程序使用Bower来管理前端依赖项。唯一没有由Bower管理的js是jQuery,因为Rails默认通过jquery-rails gem包含它。

"突然"我的开发环境停止工作,因为经过一些更新,由于某种原因,bower锁定jquery版本到3.0.0,如bower.json文件所述:

masciugo@masciugcbookpro ~/dev/v/hr_dashboard $ r bower:list I, [2016-06-23T12:58:33.549334 #5308] INFO -- : Celluloid 0.17.3 is running in BACKPORTED mode. [ http://git.io/vJf3J ] /Users/masciugo/.nvm/v0.12.4/bin/bower list bower invalid-meta The "name" is recommended to be lowercase, can contain digits, dots, dashes bower check-new Checking for new versions of the project dependencies... bower-rails generated vendor assets /Users/masciugo/dev/v/hr_dashboard/vendor/assets ├─┬ bootstrap#3.3.4 (latest is 4.0.0-alpha.2) │ └── jquery#3.0.0 ├── bootstrap-horizon#0.1.0 ├─┬ bootstrap-validator#0.9.0 (latest is 0.10.2) │ └── jquery#3.0.0 ├── classnames#2.2.3 (latest is 2.2.5) ├─┬ eonasdan-bootstrap-datetimepicker#4.17.37 │ ├── jquery#3.0.0 │ ├── moment#2.13.0 │ └─┬ moment-timezone#0.5.4 │ └── moment#2.13.0 ├── es6-shim#0.34.4 (latest is 0.35.1) ├── humps#1.0.0 (latest is 1.1.0) ├─┬ jquery-ui#1.10.4 (latest is 1.11.4) │ └── jquery#3.0.0 ├── moment#2.13.0 ├── pubsub-js#1.5.3 ├─┬ react-bootstrap#0.26.4 (latest is 0.29.5) │ └── react#0.13.3 (latest is 15.1.0) ├── react-geosuggest#1.9.1 (latest is 1.24.1) ├── spin.js#2.3.2 └── underscore#1.8.3

查看单个libs bower.json文件,它们都没有设置jquery的上限,因此下载并使用了最后一个。实际上 eonasdan-bootstrap-datetimepicker 并不适用于jquery3,因为它使用.size()方法。所以我想知道我可以强制使用较低的jQuery版本。我最终从gemfile中删除了jquery-rails并手动添加了jower(和jquery-ujs)以及bower指定版本:

{ "lib": { "name": "bower-rails generated lib assets", "dependencies": { // "threex" : "git@github.com:rharriso/threex.git", // "gsvpano.js" : "https://github.com/rharriso/GSVPano.js/blob/master/src/GSVPano.js" } }, "vendor": { "name": "bower-rails generated vendor assets", "dependencies": { "jquery": "<2", "jquery-ujs": "<2", "bootstrap": "3.3.4", "eonasdan-bootstrap-datetimepicker": "4.17.37", "bootstrap-validator": "0.9.0", "moment": "2.13.0", "underscore": "1.8.3", "react-geosuggest": "1.9.1", "pubsub-js": "1.5.3", "spin.js": "2.3.2", "jquery-ui": "1.10.4", "es6-shim": "0.34.4", "classnames": "2.2.3", "react-bootstrap": "0.26.4", "humps": "1.0.0", "bootstrap-horizon": "0.1.0" } } }

获得:

masciugo@masciugcbookpro ~/dev/v/hr_dashboard $ r bower:list I, [2016-06-27T12:09:29.493630 #21386] INFO -- : Celluloid 0.17.3 is running in BACKPORTED mode. [ http://git.io/vJf3J ] /Users/masciugo/.nvm/v0.12.4/bin/bower list bower invalid-meta The "name" is recommended to be lowercase, can contain digits, dots, dashes bower check-new Checking for new versions of the project dependencies... bower-rails generated vendor assets /Users/masciugo/dev/v/hr_dashboard/vendor/assets ├─┬ bootstrap#3.3.4 (latest is 4.0.0-alpha.2) │ └── jquery#1.12.4 (3.0.0 available) ├── bootstrap-horizon#0.1.0 ├─┬ bootstrap-validator#0.9.0 (latest is 0.10.2) │ └── jquery#1.12.4 (3.0.0 available) ├── classnames#2.2.3 (latest is 2.2.5) ├─┬ eonasdan-bootstrap-datetimepicker#4.17.37 │ ├── jquery#1.12.4 (3.0.0 available) │ ├── moment#2.13.0 │ └─┬ moment-timezone#0.5.4 │ └── moment#2.13.0 ├── es6-shim#0.34.4 (latest is 0.35.1) ├── humps#1.0.0 (latest is 1.1.0) ├── jquery#1.12.4 (latest is 3.0.0) ├─┬ jquery-ui#1.10.4 (latest is 1.11.4) │ └── jquery#1.12.4 (3.0.0 available) ├─┬ jquery-ujs#1.2.1 │ └── jquery#1.12.4 (3.0.0 available) ├── moment#2.13.0 ├── pubsub-js#1.5.3 ├─┬ react-bootstrap#0.26.4 (latest is 0.29.5) │ └── react#0.13.3 (latest is 15.1.0) ├── react-geosuggest#1.9.1 (latest is 1.24.1) ├── spin.js#2.3.2 └── underscore#1.8.3

这是一个可以接受的解决方案吗?理想情况下,我会在清单js文件中指定版本,但我从未见过。我应该如何处理这种情况?

1 个答案:

答案 0 :(得分:0)

jquery-rails特定于rails资产并且对于bower而言是“不可见的”,因此您必须在bower / bower-rails配置中指定所需的版本,以避免与bower指定的其他资产发生冲突。

您可以保留jquery-rails,因为它不会干扰bower,除非您选择在视图或布局文件中加载它(您可以引用由rails AND <直接管理的两个资产/ em>由凉亭管理的那些,但这可能会导致冲突)。