Using Rails 3.1, where do you put your “page specific” JavaScript code?

前端 未结 29 2329
一生所求
一生所求 2020-11-22 11:08

To my understanding, all of your JavaScript gets merged into 1 file. Rails does this by default when it adds //= require_tree . to the bottom of your appl

29条回答
  •  时光说笑
    2020-11-22 11:26

    I don't see an answer that really puts it all together and lays it out for you. Thus, I'll try to put meleyal, sujal (a la ClosureCowboy), the first part of Ryan's answer, and even Gal's bold statement about Backbone.js... all together in a way that is short and clear. And, who knows, I might even meet Marnen Laibow-Koser's requirements.

    Example edits

    assets/javascripts/application.js

    //= require jquery
    //= require jquery_ujs
    //= require lodash.underscore.min
    ...
    


    views/layouts/application.html.erb

      ...
      
    
      
      
      <%= javascript_include_tag "application" %>
      <%= yield :javascript %>
    
    
    
    


    views/foo/index.html.erb

    ...
    <% content_for :javascript do %>
      <%= javascript_include_tag params[:controller] %>
    <% end %>
    


    assets/javascripts/foo.js

    //= require moment
    //= require_tree ./foostuff
    


    assets/javascripts/foostuff/foothis.js.coffee

    alert "Hello world!"
    


    Brief description

    • Remove //= require_tree . from application.js and list only the JS that each page shares.

    • The two lines shown above in application.html.erb tell the page where to include application.js and your page-specific JS.

    • The three lines shown above in index.html.erb tells your view to look for some page-specific JS and include it at a named yield region called ":javascript" (or whatever you want to name it). In this example, the controller is "foo" so Rails will attempt to include "foo.js" at the :javascript yield region in the application layout.

    • List your page-specific JS in foo.js (or whatever the controller is named). List common libraries, a tree, directories, whatever.

    • Keep your custom page-specific JS someplace where you can easily reference it apart from your other custom JS. In this example, foo.js requires the foostuff tree so put your custom JS there, such as foothis.js.coffee.

    • There are no hard rules here. Feel free to move things around and perhaps even create multiple yield regions of various names in various layouts if needed. This just shows one possible first step forward. (I don't do it exactly like this given our use of Backbone.js. I might also choose to drop foo.js down into a folder called foo instead of foostuff but haven't decided that yet.)

    Notes

    You can do similar things with CSS and <%= stylesheet_link_tag params[:controller] %> but this is beyond scope of the question.

    If I missed a glaring best practice here, send me a note and I'll conisder adapting. Rails is fairly new to me and, honestly, I'm not terribly impressed so far with the chaos it brings by default to enterprise development and all the traffic the average Rails program generates.

提交回复
热议问题