hyperrjas
hyperrjas

Reputation: 10744

method is not defined on backbone.js + coffeescript

I have my javascript in backbone.js with coffeescript instead javascript:

TodoItem = Backbone.Model.extend(
  toggleStatus ->
    if @.get 'status' is "incomplete"
      @.set 'status': 'complete' 
    else
      @.set 'status': 'incomplete'
    @.save()  
)

todoItem = new TodoItem(
  description: 'Play the guitar'
  status: 'incomplete'
  id: 1
)

TodoView = Backbone.View.extend(
  tagName: 'div'
  id: "box"
  className: 'red-box'

  template: 
    _.template "<h3> <input type=checkbox #{ print "checked" if status is "complete"} /> <%= description %></h3>"

  events: 
    "click h3": "alertStatus"
    'change input': 'toggleStatus'

  toggleStatus: ->
    @.model.toggleStatus()

  alertStatus: ->
    alert('Hey you clicked the h3!')

  render: ->
    @.$el.html @.template(@.model.toJSON())
)

todoView = new TodoView({model: todoItem})
todoView.render()
console.log todoView.el

Backbone version is the last version 0.9.10 and underscore.js version is last version 1.4.4

The coffeescript file compile fine however I get in console:

Uncaught ReferenceError: toggleStatus is not defined main.js:5

(anonymous function) main.js:5

(anonymous function)

Thanks!

Upvotes: 1

Views: 327

Answers (1)

romualdr
romualdr

Reputation: 184

I guess that you're trying to assign a anonymous function to your object at key toggleStatus at line 2.

Then, you just forget the : when declaring toggleStatus.

Upvotes: 2

Related Questions