开发者

Rails 3.1 recommended practice for managing library javascripts [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed last year.

Improve this question

I'm curious what is the recommend approach for organizing and using library javascripts (things like Backbone, Underscore, Modernizr, etc.) in Rails. I've noticed a number of different practices so it seems to get a bit confusing.

There are a number of gems that provide the ability to bundle things like Modernizr, Backbone, Underscore, etc. Is this a preferred approach for cases where a gem exists?

For cases where a gem doesn't exist would it make sense to use the /public folder or the /app/assets/javascripts and go v开发者_开发知识库ia the pipeline?


Just to add something (the answer of @DevinM is wonderful), here are my thoughts about using JavaScript in Rails:

  • If there is a Gem that bundles a JavaScript library (like jquery-rails) I would stick to that:
    • Keeps the library away from your code.
    • Adds the libraries automatically to rails (or at least explains how to do that).
    • Helps you to update the library automatically through bundle update if you want to.
  • If there is no integration by a Gem, keep it under vendor/assets as described:
    • Ensures that the library is known as external.
    • Document the need to that library somewhere (e.g. in your application.js file)
  • Your own libraries in lib/assets as described.
  • application.js or the controller dependent *.js files in your app/assets/javascripts folder.
  • Use application.js mostly for wiring, that means how to integrate the other libraries into your application. Explain there what the code means: used library, reference to documentation, meaning of configuration.

My experience is that after some time, you don't remember what the reason for a library was, so some house-keeping helps you stay healthy.


I would take a look at the Rails Guide for this question.

Assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.

app/assets is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.

lib/assets is for your own libraries’ code that doesn’t really fit into the scope of the application or those libraries which are shared across applications.

vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins.

What I would do is place all the JS files that you dont maintain is vendor/assets, if this folder is missing you can just create it. And as far as using gems for your assets thats usually fine for larger frameworks and libraries like jQuery however I would shy away from them for stuff like Backbone.js and others just to keep the gemfile clean and reduce some bloat you might see with that.

If you need any clarification let me know and good luck with your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜