Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling JavaScript function from CoffeeScript file

I need to use a JavaScript library in my CoffeeScript application. Since I am not familiar with both languages I try something simple. My coffeescript file:

empty = require('models/empty')

    class Contact extends Spine.Model
      @configure 'Contact', 'name', 'email'

      @extend Spine.Model.Local

      create: -> 
        empty.one()
        super

    module.exports = Contact

And my Javascript file named empty.js :

console.log('what')

function one () {
    console.log('one')
};

The coffeescript file works normally, although I cant get empty.one() to work. 'what' is printed on console which means that the JS file is loaded. Although I get the following error when one() is called:

Uncaught TypeError: Object # has no method 'one'

I have tried many different ways of defining the function, as variable, and using different syntaxes I found on tutorial, although none of this seems to work. Can someone point the mistake I am making?

like image 247
Giannis Avatar asked May 04 '26 14:05

Giannis


1 Answers

You need to export the function like this:

function one () {
    console.log('one')
};
exports.one = one;

Then it will be accessible from other modules that require it.

(I assume that you use node.js or any other commonjs-like platform)

like image 171
Sascha Gehlich Avatar answered May 06 '26 02:05

Sascha Gehlich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!