Search code examples
backbone.jscoffeescriptjsfiddle

Coffeescript in Jsfiddle Uncaught SyntaxError


I'm having the following error in jsfiddle using backbone, underscore and coffeescript: http://jsfiddle.net/bx7g7d2y/3/

It seems my code is OK, the error raises in coffeescript file, line 8.

This is my demo code:

# extending a simple model
SidebarModel = Backbone.Model.extend {
    initialize: -> {
        console.log 'initialized'
    }
}

sidebar = new SidebarModel

Can you spot something that I'd missing ?

UPDATE: By the other hand, it works neat with javascript:

// extending a simple model
var SidebarModel = Backbone.Model.extend ({
    initialize: function(){
        console.log('initialized');
    }
})

var sidebar = new SidebarModel();

Solution

  • Try the following in the Try CoffeScript-tab on the CoffeScript website.

    SidebarModel = Backbone.Model.extend {
        initialize: -> 
            console.log 'initialized'
    }
    
    sidebar = new SidebarModel
    

    -> is the CoffeScript-literal for a function. No brackets needed for that!

    You could even go one step further and write:

    SidebarModel = Backbone.Model.extend 
        initialize: -> 
            console.log 'initialized'
    
    sidebar = new SidebarModel