What’s the best practice for storing and referencing JS for admin section of site?
Currently, I have an application.js that references a lot of JS (both vendor and app). I’d like to move some of the references into another file so it’s not loaded (unnecessarily) when a user is not admin.
I’m looking for the following behavior:
- If user is not admin, load everything in application.js including all files under
/assets/javascriptsvia//= require_tree . - If user is admin, load everything that a regular user would load. In addition, load some specific JS. I’m not sure where to store the JS since I don’t want it to be picked up by
require_tree
Any suggestions on where to store the code, and how to call it would be greatly appreciated.
Maybe you can do something like this. Create a
admin_application.jsfile that requires all your.jscode you need for the admin users, excluding therequire_treedirective, and maintain an unchangedapplication.jsthat has all the requires and stuff for non-admin users.If they share a common layout, it would be easy enough to put some logic inside your header tag to handle the two cases. Example:
application.html.erb
I think this is a easy and fair approach to your problem.