1

我在backbone.js中有我的javascript,用的是coffeescript而不是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

咖啡脚本文件编译得很好,但是我进入控制台:

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

(匿名函数)main.js:5

(匿名函数)

谢谢!

4

1 回答 1

2

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.

于 2013-02-02T12:40:50.753 回答