0

From another thread here I found this great tutorial on markup based JS execution Garber-Irish solution:

http://www.viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution/.

I'm checking out how I can do parts of this in Coffeescript.

This works OK:

SITENAME.surveys.show = ->
  alert "Hello CoffeeScript"

Which renders out:

SITENAME.surveys.show = function() {
  return alert("Hello CoffeeScript");
};

This one is not so happy:

SITENAME.surveys.new = ->
  alert "Hello CoffeeScript"

SITENAME.surveys["new"] = function() {
  return alert("Hello CoffeeScript");
};

I'm new to Coffeescript and doing a codeschool.com course on it now. I guess the "new" keyword is special for coffeescript.

Is there any workaround for this?

Thanks!

Rudi Starcevic
  • 645
  • 1
  • 11
  • 17

2 Answers2

2

new is special in JavaScript and CoffeeScript is aware of this so it's emitting code that will actually work even though it's bad practice to name methods using reserved keywords.

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
2

If you need to use new, you can use [] to define the function:

SITENAME.surveys['new'] = ->
  alert "Hello CoffeeScript"

and to call it:

SITENAME.surveys['new']()

Demo: http://jsfiddle.net/ambiguous/Y3qnt/

A quick review of your link suggests that you'll be accessing the function with something like:

controller = 'surveys'
action     = 'new'
SITENAME[controller][action]()

So it doesn't matter what the methods are called as you'll always be referring to them by their (string) name anyway.

mu is too short
  • 426,620
  • 70
  • 833
  • 800