Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6115665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:07:04+00:00 2026-05-23T15:07:04+00:00

TLDR: What can you do to combine multiple CoffeeScript files into one JS file,

  • 0

TLDR:

What can you do to combine multiple CoffeeScript files into one JS file, in RoR, all under the same anonymous function block?

Long version:

I have a few CS files that will be loaded for part of a RoR web app. I’m wondering: what is a good way to separate concerns with CoffeeScripts and Ruby on Rail 3.1’s asset pipeline?

Let’s use the following as example code:

main.js.coffee

window.MyApp = {} # to escape the CoffeeScript anonymous function block
# (I like the anonymous function block because it protects my other

MY_GLOBAL_SETTING = "world!"
$.click "#my_button" myApp.sayHello
# (I could use something like goog.bind here instead of using myApp. Any suggestions? Fat arrow?)

hello.js.coffee

MyApp.sayHello = sayHello () ->
  doComplicatedStuff()
  alert("Hello #{ MY_GLOBAL_SETTING }")

complicated.js.coffee

doComplicatedStuff = () ->
  # some really complicated algorithm, for example
  true

I have my assets directory structured like the following:

assets/
  application.js
  application/
    # javascript that gets used with the main application
  secondary_page.js
  secondary_page/
    complicated.js.coffee
    hello.js.coffee
    main.js.coffee

secondary.js

//= require secondary_page/main.js.coffee
//= require secondary_page/complicated.js.coffee
//= require secondary_page/hello.js.coffee

I used to compile the files together with CoffeeScript as part of the build process, but now I want to use the asset pipeline instead. I’m drinking the RoR 3.1 kool-aid! Haha, seriously though, the asset pipeline looks awesome.

The problem I’m experiencing is that secondary.js looks like the following:

(function() {
  // main.js
).call(this);
(function() {
  // complicated.js
).call(this);
(function() {
  // hello.js
).call(this);

This prevents local variables from being shared amongst the entire code. MY_GLOBAL_SETTING and doComplicatedStuff aren’t available to sayHello.

So… what should I do? I can’t think of a good way without introducing my own custom compilation step again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:07:04+00:00Added an answer on May 23, 2026 at 3:07 pm

    This is a common question for Rails developers starting to use CoffeeScript. See, for example:

    • "Can't find variable" error with Rails 3.1 and Coffeescript
    • Functions in controller.js.coffee
    • How can I use option "–bare" in Rails 3.1 for CoffeeScript?

    Solutions abound. The simplest is to preface variable declarations that you want to be visible outside of a particular file with @, since the this will point to window in the outermost context of each file, and x points to window.x when no local x is defined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

TLDR I need to change a javascript variable on the same page after clicking
TLDR I have a function that runs on the end of a pan in
TLDR I want to add some dynamic content into an Ext-JS window / popup.
TLDR : Can the onreadystatechange event be emulated somehow else for synchronous requests without
I can't figure out if this is a bug in Firefox or an anomaly
TLDR : I have an Openlayers map with a layer called 'track' I want
TLDR Linking my fresh boost build with Visual Studio (/Mtd) causes boost to throw
if (reader.is_lazy()) goto tldr; I have a background thread that does some I/O-intensive background
TLDR version: Most important issue is, that in a TSP problem, instead of finding
I have a user defined function in a SQL Server 2005 database which returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.