0
我有這樣綁定這Backbone.js的
Friend = Backbone.Model.extend
name:null
Friends = Backbone.Collection.extend
initialize: (models,options)->
this.bind("add",options.view.addFriendLi)
AppView = Backbone.View.extend
el: $("body")
initialize: ->
this.friends = new Friends(null,view:this)
_.bindAll(@,"addFriendLi")
events:
"click #add-friend": "showPrompt"
showPrompt: ->
friend_name = prompt("who is your friend?")
friend_model = new Friend(name:friend_name)
this.friends.add(friend_model)
addFriendLi : (model) ->
console.debug this.friends #returns undefined
if model.get("name")?
item = $("<li>"+model.get("name")+"</li>")
item.appendTo("#friends-list")
appview = new AppView
一個簡單的演示應用程序這除了將內addFriendLi
this.friends返回undefined大部分工作正常。這意味着這不是綁定到當前的模型實例。但我按照說明並撥打_.bindAll(this,"addFriendLi")
。我不明白爲什麼這是行不通的。