简体   繁体   中英

how to organize javascripts using rails and jquery

i'm working in a big and rich rails web application using tons of javascript. I would like to know if anybody has a tip to organize the javascripts. Today i'm generating a new file named controller.js and adding it to my views using content_for. The problem is, some files are becoming big and sometimes, i need a function from one controller in another, so then in the end, i add a products.js to a details controller just to keep DRY. Is that solution good? Any other tip? I think the same pattern can be applied as well to css files?

I actually just posted my current approach to organising JS in Ruby on Rails .

I have a namespaced JavaScript file for each of my controllers and then I use asset_packager to bind these many small files into a single file at deploy time.

You could make this more granular and have a file for each action in a controller, but it means I have many small files scoped to small elements of functionality in the my system.

Works really well.

You've got conflicting demands here because you want your JavaScript files to be nice and modular for maintainability during development, but in production you want them to be combined to minimize the number of HTTP requests made, thus improving performance.

Personally I've found Jammit to be a good solution for managing these apparently conflicting aims. It's a gem that packages your assets (CSS and JavaScript) and as a bonus can compress them too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM