Giannis
Giannis

Reputation: 5526

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?

Upvotes: 3

Views: 2855

Answers (1)

Sascha Gehlich
Sascha Gehlich

Reputation: 1007

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)

Upvotes: 6

Related Questions