1

I am using rails-backbone, coffeescript gems in my rails 3.2.6 project.

square = (x) -> x * x alert square(5)

this is the blog.js.coffee script file it produces:

(function() { var square; square = function(x) {return x * x;}; alert(square(5));

I need to call the square() method in an other view file.

How can I call that? Is there any thing wrong I am doing?

edi9999
  • 19,701
  • 13
  • 88
  • 127
Ponnusamy
  • 87
  • 1
  • 1
  • 6

2 Answers2

2

All your code in Coffeescript will be inside a self-invoking anonymous function.

To call it outside a file, just write:

window.square = (x) -> x * x 

alert(square(5)) in an other function

The best you can do to not overuse window is a App object that will contain all your variables.

window.App={}
window.App.square=  (x) -> x * x 

and then alert(App.square(5))

edi9999
  • 19,701
  • 13
  • 88
  • 127
  • What is Self-anonymous function? – Ponnusamy Jul 25 '13 at 04:13
  • A self invoking anonymous function is a function that has no name (**hence anonymous**) and that calls itself. It is used to protect the code from being accessed from the outside of the function. Here's a specifi question about this. – edi9999 Jul 25 '13 at 08:14
-1

Call it like a regular JavaScript function:

<script>    
  square(5)
</script>
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261