Call coffeescript function from jQuery function

2019-08-19 03:53发布

问题:

I have two files: first one is a plain jQuery, and a second one is a Coffeescript

jQuery file:

$(document).ready(function(){
    checkPrice();
});

CoffeeScript file:

$ ->
   checkPrice = ->
     alert("OK");

I get following error: "Unhandled Error: Undefined variable: checkPrice"

In the template they are included in opposite direction: coffeescript file, then jquery file.

Is there some way to make them working together ?

回答1:

OK. Got it.

checkPrice should have been declared as global, so in coffeescript file I have:

window.checkPrice =->
  alert("OK");

And now it works!

Thank's everyone who was there to help me anyway.