How to import javascript in rails with asset pipeline -


in app have 1 controller supporting quite complex object has lot of javascript, written in coffescript.

i arrange javascript on several separate files have code arranged more nicely, although can't figure out how import these files.

for example have file app/assets/javascripts/general_functions.js.coffee containing following:

# rounds number roundnumber = (rnum, rlength = 5) ->   pow = math.pow( 10, rlength )   newnumber = math.round(rnum*pow)/pow   parsefloat(newnumber)  # floors number floornumber = (rnum, rlength = 5) ->   pow = math.pow( 10, rlength )   newnumber = math.floor(rnum*pow)/pow   parsefloat(newnumber)  # returns true if str ends suffix endswith = (str, suffix) ->   str.indexof(suffix, str.length - suffix.length) !=   -1  # returns absolute value of number (always >= 0) abs = (num) ->    if num < 0 - num else num 

how import in app/assets/javascripts/projects.js.coffee needs these functions?

i've tried adding

//= require general_functions 

to app/assets/javascripts/application.js, no success

any ideas?

thanks,

by no success i'm guessing browser telling none of general_functions.js.coffee functions exist , you're getting errors like:

referenceerror: roundnumber not defined

you have simple scoping issue. compiled version of coffeescript files wrapped in self-executing function prevent namespace pollution this:

roundnumber = (rnum, rlength = 5) ->   pow = math.pow( 10, rlength )   newnumber = math.round(rnum*pow)/pow   parsefloat(newnumber) 

looks when gets browser:

(function() {   var roundnumber;   roundnumber = function(rnum, rlength) {     // ...   }; })(); 

and functions you've defined hidden. if want functions global, define them window properties:

window.roundnumber = (rnum, rlength = 5) ->   # ... 

or better, can create application-specific namespace somewhere before main (coffee|java)script loaded:

app = { } 

and put functions in there:

app.roundnumber = (rnum, rlength = 5) ->   # ... 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -