Javascript functions in the rails 4 asset pipeline tree -


i'm trying utilize javascript function via chrome console when put javascript function within rails asset pipeline manifest. here steps i've take create , setup simple rails 4.2.4 app

$ rails new javascriptexample $ cd javascriptexample $ rails g scaffold bear name:string $ rake db:migrate 

i edit app/assets/javascripts/bears.coffee , add console log , function.

console.log("asset pipeline sucks") square = (x) -> x * x 

then fire server

$ rails s 

i visit localhost:3000/bears , in chrome console see first line of code has worked. when attempt command square(5); in console receive error uncaught referenceerror: square not defined(…)

why can not way when function loaded application.js?

this coffeescript compiled javascript

(function() {   var square;    console.log("asset pipeline sucks");    square = function(x) {     return x * x;   }; }).call(this); 

from this: the var keyword reserved in coffeescript, , trigger syntax error if used. local variables created implicitly default, not available in global scope expectation

to make work, can instead:

console.log("asset pipeline sucks") @square = (x) -> x * x 

note have @ , compiled javascript be:

(function() {   console.log("asset pipeline sucks");    this.square = function(x) {     return x * x;   };  }).call(this); 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -