loading order JavaScript files in asset pipeline

我的未来我决定 提交于 2019-12-06 08:19:24

问题


I am using a Rails 4 application. I installed some JavaScript plugin files in "vendor/assets/javascripts", and some in "app/assets/javascripts" where I have some files that are invoking methods from the vendor files.

No methods from these are recognized and it seems that "app/assets/javascripts" files are loaded before "vendor/assets/javascripts" files. How can I deal with that?

For information my "application.js" calls:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require rails.validations
//= require_tree .

and I am using gem 'jquery-rails'.


回答1:


Any files in your vendor tree that you need must be required explicitly in your "application.js". It will pull in all the JavaScript in your "app/assets/" tree using the "require_tree".

You probably need to update your file to look something more like this:

//= require jquery
//= require jquery_ujs 
//= require turbolinks
//= require rails.validations
//= require that_file_from_vendor_assets
//= require that_other_file_from_vendor_assets
//= require_tree .

where that_file_from_vendor_assets is the vendor JavaScript that you need to be loaded up before it gets to your "app/assets/javascript" files.




回答2:


Just change

//=require_tree .

like this:

//= require_self

Then files will be imported in the order of you put them to your application.js file.

And don't forget <%= javascript_include_tag "application"%> in your layout file.



来源:https://stackoverflow.com/questions/21939818/loading-order-javascript-files-in-asset-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!