方法没有在backbone.js + coffeescript上定义

时间:2013-02-02 12:09:13

标签: javascript jquery backbone.js coffeescript

我的 backbone.js 中包含 coffeescript 的javascript,而不是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版本是最后一个版本0.9.10,而underscore.js版本是最后一个版本1.4.4

coffeescript文件编译正常,但我进入控制台:

未捕获的ReferenceError:未定义toggleStatus main.js:5

(匿名函数)main.js:5

(匿名函数)

谢谢!

1 个答案:

答案 0 :(得分:2)

我猜你正试图在第2行的密钥toggleStatus为你的对象分配一个匿名函数。

然后,您在声明:时忘记toggleStatus