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

The new Rails 3.1 pipeline is really good, but since all CoffeeScript (or JavaScript) files are combined into a single file, which is included on each page, the question arises:

How to limit the execution of my script to a specific controller or action? Is there a way in my CoffeeScript to find out which controller and action was used during the request so that I can put conditional statements in my script?

Or am I even approaching this wrong path?

+9
javascript ruby-on-rails coffeescript
May 26 '11 at 11:27
source share
5 answers

Trevor Burnham answers this question here: How do I link a CoffeeScript file to a view?

He says:

There are two general approaches:

  • Make the behavior conditional on the presence of a specific element. For an instance, the code to run the registration sheet should be preceded by something like

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

  • Make the behavior conditional for the class in the body element. You can set the body class with ERB. This is often desirable for style sheets, like Well. The code will be something like

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

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

+8
May 27 '11 at 11:30 a.m.
source share

One way to restrict coffeescript to a specific kind is to create your own sprockets file for the javascript in question, similar in application.js format. Let's say you call it extras.js.

// = my_code.js.coffee required

Then use javascript_include_tag "extras" to include this code in the views you need, either by creating your own layout for these views, or using content_for ()

By the way, your question said that the rail pipeline forces you to put all your js assets in one file. It is not true. This often helps to avoid multiple trips, but you can have multiple star files.

+1
Jul 20 2018-12-18T00:
source share

Why not put javascript for a specific controller as a representation on that controller (since they match there if they exist)?

If they are common, you can mark your presentation with classes, identifiers or data (html5) and make your own JavaScript code for this (so you can reuse your code).

0
May 26 '11 at 17:47
source share

what I usually do is get income: js under javascripts in my layout, and when I need a specific script, I load it directly from my view with:

 content_for :js do javascript_include_tag "myscript" end 
0
Mar 15 '12 at 9:23
source share

If you are using a gon gem for your vars in a coffee script, you can use this template:

Place a flag for each 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 a correlating coffee script, use these flags to distinguish between 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
Oct 05 '14 at 8:31 on
source share



All Articles