主干错误,对象添加没有方法“绑定”;
items.bind 'add', (item) =>
@addOne(item)
addOne: (item) ->
view = new ListItem({model: item})
..视图的初始化抛出此错误: TypeError: Object add has no method 'bind'
class ListItem extends Backbone.View
el: $ '#wrap'
template: $ '#listItem'
initialize: () ->
@model.bind 'change', @render, @
@model.bind 'destroy', @remove, @
render: () ->
@el.append @template.tmpl @model.toJSON()
EDIT:logging item before set the view results in...
Item
_callbacks: Object
_changed: false
_changing: false
_escapedAttributes: Object
_previousAttributes: Object
attributes: Object
cid: "c2"
collection: Items
__proto__: ctor
但是如果我在初始化视图时记录@model,它是一个名为 add
编辑 的空对象2: item.bind 在 addOne 中未定义,但不知道为什么
items.bind 'add', (item) =>
@addOne(item)
addOne: (item) ->
view = new ListItem({model: item})
..initialization of the view throws this error: TypeError: Object add has no method 'bind'
class ListItem extends Backbone.View
el: $ '#wrap'
template: $ '#listItem'
initialize: () ->
@model.bind 'change', @render, @
@model.bind 'destroy', @remove, @
render: () ->
@el.append @template.tmpl @model.toJSON()
EDIT: logging item before setting the view results in...
Item
_callbacks: Object
_changed: false
_changing: false
_escapedAttributes: Object
_previousAttributes: Object
attributes: Object
cid: "c2"
collection: Items
__proto__: ctor
but if I log @model when initializing the view it's an empty object called add
EDIT 2: item.bind is undefined from within addOne, not sure why though
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码看起来不错,但您添加到
items
的对象似乎不是有效的模型。您的items.add
调用使用什么代码?验证运行items.add(new Backbone.Model)
时不会出现错误。也许您确实这样做了,
但将
Collection
类(items
是其实例)上的model
属性设置为除Backbone.Model 之外的其他内容子类?
Your code looks fine, but the object that you've added to
items
doesn't appear to be a valid model. What code are you using for youritems.add
call? Verify that you don't get an error when you runitems.add(new Backbone.Model)
.Perhaps you did
but set the
model
property on theCollection
class thatitems
is an instance of to something other than aBackbone.Model
subclass?