How do you limit CoffeeScript (or JavaScript) execution to a particular controller and action in Rails 3.1?

前端 未结 5 607
攒了一身酷
攒了一身酷 2021-01-02 09:50

The new Rails 3.1 asset pipeline is really nice, but since all CoffeeScript (or JavaScript) files get melded down into a single file that is included in every page, it raise

相关标签:
5条回答
  • 2021-01-02 10:18

    One way to restrict coffeescript to a particular view is to make a custom sprockets file for the javascript in question, similar in format to application.js. Say you call it extras.js.

    //= require my_code.js.coffee

    Then use javascript_include_tag "extras" to include that code in the views you want, either by making a custom layout for those views, or by using content_for()

    BTW, your question stated that the rails pipeline forces you to put all your js assets in one file. That's not true. That's efficient often to avoid multiple round trips, but you can have multiple sprocket files.

    0 讨论(0)
  • 2021-01-02 10:21

    If you are using the gon gem for your vars in coffee script you can use this pattern:

    Put a flag for every action in the controller:

      def index
        @gps_coords = GpsCoord.all
    
        # Flag for the CoffeeScript to decide which part to run
        gon.index = true;
      end
    
      def show
        @gps_coord = GpsCoord.find(params[:id])
    
        gon.lat = @gps_coord.latitude
        gon.lon = @gps_coord.longitude
    
        gon.show = true;
      end
    

    In the correlating coffee script use those flags to distiguish between the both actions:

      # index action?
      if gon.index? 
        first_coord = gon.gps_coords[0]
        map.setView([first_coord.latitude, first_coord.longitude], 15);
    
      # show action?
      if gon.show?
        map.setView([gon.lat, gon.lon], 15);
    
    0 讨论(0)
  • 2021-01-02 10:36

    Trevor Burnham answers this question nicely here: How do I associate a CoffeeScript file with a view?

    He says:

    There are two common approaches:

    1. Make behavior conditional on the presence of a particular element. For instance, code to run a signup sheet should be prefaced with something like

      if $('#signup').length > 0

    2. Make behavior conditional on a class on the body element. You can set the body class using ERB. This is often desirable for stylesheets as well. The code would be something like

      if $('body').hasClass 'user'

    And if you're interested in CoffeeScript, Trevor is working on a book that looks to be very good: http://pragprog.com/titles/tbcoffee/coffeescript

    0 讨论(0)
  • 2021-01-02 10:40

    Why not to put the javascript for the particular controller as a view on this controller (as they correspond there if are so specific)?

    If they are general enaugh you can mark your view with classes, ids or data (html5) and make your javascript look for that (so you can reuse your code).

    0 讨论(0)
  • 2021-01-02 10:40

    what i normally do is to have a yield :js under the javascripts in my layout and when I need a specific script it, I load it directly from my view with:

    content_for :js do
        javascript_include_tag "myscript"
    end
    
    0 讨论(0)
提交回复
热议问题