我試圖用骨幹與CoffeeScript的替代的javascript:的ReferenceError:的TodoItem沒有定義骨幹+ CoffeeScript的
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。
它在哪裏出錯?
謝謝!
Afaik下劃線使得所有的變量都是本地的,所以你不能從控制檯訪問它們而沒有聲明它們是全局的。 – Bergi
你究竟是什麼意思,「*我不能看到我的身體裏面的div *」? – Bergi
謝謝,我的意思是''div id =「box」class =「red-box」> ...'不會在我的html頁面中呈現。你可以粘貼一個例子來解決這個問題嗎?謝謝! – hyperrjas