我正在尝试将骨干与咖啡脚本一起使用,而不是 javascript:
TodoItem = Backbone.Model.extend(
toggleStatus: ->
if @.get 'status' is "incomplete"
@.set 'status': 'complete'
else
@.set 'status': 'incomplete'
@.save()
)
todoItem = new TodoItem(
description: 'I play the guitar'
status: 'incomplete'
id: 1
)
TodoView = Backbone.View.extend(
tagName: 'div'
id: "box"
className: 'red-box'
events:
"click h3": "alertStatus"
'change input': 'toggleStatus'
template:
_.template "<h3> <input type=checkbox #{ print "checked" if status is "complete"} /> <%= description %></h3>"
initialize: ->
@.model.on 'change', @.render, @
@.model.on 'destroy', @.remove, @
toggleStatus: ->
@.model.toggleStatus()
alertStatus: ->
alert('Hey you clicked the h3!')
remove: ->
@.$el.remove()
render: ->
@.$el.html @.template(@.model.toJSON())
)
todoView = new TodoView({model: todoItem})
todoView.render()
console.log todoView.el
如果我在控制台中尝试:
todoItem.set({description: 'asdfadfasdfa'});
我得到:
ReferenceError: todoItem is not defined
此外,我看不到我体内的 div:
<div id="box" class="red-box">
<h3>
<input type="checkbox" undefined>
"I play the guitar"
</h3>
</div>
但我可以在我的控制台中看到这个 div 很好。
错误在哪里?
谢谢!