[SOLVED] How to make a function call in "init.coffee"?


#1

Hello.

Recently I wrote a little piece of code that triggers a comment line. The code piece was placed in init.coffee and bound to a key combination keymap.cson.

As reference Option to insert comment characters at the beginning of the line, rather than just before the first character?:

Is it possible to have this code piece in another (external) file and have init.coffee point to this file? How is that done?
:slightly_smiling_face: (Please - without creating a custom Package)

Thanks.
- Dan Padric


#2

I have not done this and I don’t know if it’s possible (and I don’t have time to test at the moment), but if it were possible, you would use require() per Node’s module rules to import what you’ve written in that other file (and put in export).


#3

Thank you! that was very helpful. My feedback follows…

A simple example was referenced:
http://www.giantflyingsaucer.com/blog/?p=4162

The designed code looks like:

Focusing on the code:

# test1.coffee
module.exports = class Hello
  greet: () ->
    console.log 'Hello World!'
# init.coffee
atom.commands.add 'atom-text-editor', 'test command', ->
  Hello = require("./customTools/test1")
  console.log 'Follow test version 1.2'    # This line is optional
  hello = new Hello()
  hello.greet()

I hope this will be of help to others too.

Regards.
- Dan Padric


#4

Hello.

For others that are like me starting from ground-level…
Importing into Coffeescript from Javascript is also possible.

Some variations on the theme are given next.

(1) Coffeescript class -> Coffeescript

### "./customTools/test1.coffee"  ###

### "init.coffee"
atom.commands.add 'atom-text-editor', 'test command', ->
  Hello = require("./customTools/test1.coffee")
  console.log 'Are you there Test1?'
  hello = new Hello()
  hello.greet()
###

module.exports = class Hello
  greet: () ->
    console.log 'Hello World!'

(2) Javascript function -> Coffeescript

// "./customTools/test2.js 
/*
### init.coffee ###
atom.commands.add 'atom-text-editor', 'test command', ->
  Hello = require("./customTools/test2.js")
  console.log 'Are you there Test2?'
  Hello()
*/

module.exports = function () {
  console.log("Hello there from Test2.")
}

(3) Javascript function -> Coffeescript

// "./customTools/test3.js 
/*
### init.coffee ###
atom.commands.add 'atom-text-editor', 'test command', ->
  Hello = require("./customTools/test3.js")
  console.log 'Are you there Test3?'
  hello = new Hello()
  hello.greet()
*/

class Hello {
//   constructor() {   // Not required for this task
//   }

  greet() {
    console.log("Hello there from Test3.")
  }
}

module.exports = Hello

(2) Multi Javascript functions -> Coffeescript

// "./customTools/test4.js 
/*
### init.coffee ###
atom.commands.add 'atom-text-editor', 'test command', ->
  {hello, byeNow} = require("./customTools/test4.js")
  console.log 'Are you there version 4?'
  hello()
  byeNow()
*/

function hello() {
  console.log("Hello there from Test4")
}

function byeNow() {
  console.log("Test4: \"Have a nice day\"")
}

module.exports = {hello, byeNow}

Some example code using this idea can be seen in: [LINK]

Greetings.
- Dan Padric


Can't get the loop working in init.coffee
#5

This is because CoffeeScript gets translated into JavaScript before it is run. You’re not actually importing into CS, since the translation happens first. There are some circumstances where putting JS inside a CS file will cause the translator to get confused, but I use it a lot specifically for circumstances where () make functions more readable (especially if you’re invoking more than one in a line).