-1

I am new to coffee scripting..This is my first script and is not working..

html
 head title='hello world'
  css:
   h1{
    color: blue;
    }
  coffee:
     number   = 42
     opposite = true
     saybye = () ->
      alert 'hello'+number
      ''        
body
  h1 
   |hello world
  input type="button" onclick="saybye()" value="sayhello"

scss:
 $blue: #3bbfce;

 h2{
  color: $blue;
 } 

It says saybye is not found and should I always end with some expression which will get return. Is there a way to stop the return call?

ylluminate
  • 12,102
  • 17
  • 78
  • 152
coool
  • 8,085
  • 12
  • 60
  • 80

1 Answers1

3

I don't know what Slim does but CoffeeScript usually gets wrapped in a function to avoid namespace pollution. So, your CoffeeScript probably ends up being converted to JavaScript something like this:

(function() {
  var number, opposite, saybye;

  number = 42;

  opposite = true;

  saybye = function() {
    alert('hello' + number);
    return '';
  };
})();

The result is that saybye is not visible to your HTML.

You really shouldn't be using onclick in 2012, you should be binding to the event through the modern API. If you're using jQuery, you'd do it like this:

coffee:
  number   = 42
  opposite = true
  saybye = () ->
    alert 'hello'+number
    ''  
  $ -> $('input[type=button]').click saybye

If you're not using jQuery (or similar), then you'd do it the hard way using addEventListener on the raw DOM object. Alternatively, you could put them in window yourself and bypass scope protection:

coffee:
  window.number   = 42
  window.opposite = true
  window.saybye   = () ->
    alert 'hello' + number

As far as returning something goes, don't worry about it, return whatever makes sense. Sometimes returning undefined makes sense, sometimes it doesn't. If there is no obvious return value then just let CoffeeScript do what it wants, go with this:

saybye = () ->
  alert 'hello' + number

and move on to more interesting problems.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • are these variables not part of the window array ..?? so if I have variable defined with coffee: and I have a regular script using script then it will not have access to that variable...as you see I mentioned that I am learning coffee..and I didn't want to complicate it with jquery...so the onclick. – coool Jan 20 '12 at 18:56
  • @coool: No, they're wrapped in a `(function(){...})()` specifically to avoid polluting the global (AKA `window`) scope. You could put them into `window` yourself though, I've added an update to cover that. – mu is too short Jan 20 '12 at 19:11
  • @coool: Not that I know of and such a default would just make it easier to shoot yourself in the foot; we already have more than enough ways to cause problems for ourselves, we don't need another. – mu is too short Jan 20 '12 at 19:46
  • Here is a short cut rather than saying the entire window. use @saybye – coool Jan 20 '12 at 19:53
  • You're probably better off using an explicit `window` rather than the context sensitive `@` (AKA `this`). – mu is too short Jan 20 '12 at 20:00
  • do you how we can set -b option in sinatra for coffee-script – coool Jan 20 '12 at 21:13