示例: http: //franklovecchio-playback.herokuapp.com/?log=true
完整代码:https ://github.com/franklovecchio/playback
您会看到“未定义”属性出现在错误中:Uncaught TypeError: Object #<Thing> has no method 'serializeData'
在我返回解析覆盖的模型之后。
最初,我将 Collection 绑定到 CompositeView:
things = new App.Collections.Things()
things.fetch()
# Render
App.Pages.State.Action1.getLayout().content.show new App.CompositeViews.Action1Things(
collection: things
)
在集合parse
方法中,我fetch
提供了有关模型的更详细信息(返回一个名为 的属性extra
,该属性呈现为未定义:
class App.Collections.Things extends Backbone.Collection
url: '/things'
model: App.Models.Thing
@trace initialize: () ->
# Override response to get more detailed information about thing.
@trace parse: (resp) ->
things = []
_.each resp, (item) =>
# Fetch detailed information about thing
thing = new App.Models.Thing(
id: item.id
name: item.name
)
thing.fetch()
things.push thing
things
然后,在模型内部:
class App.Models.Thing extends Backbone.Model
defaults:
name: 'n/a'
extra: 'n/a'
urlRoot: () ->
'/thing'
defaults:
name: null
@trace initialize: () ->
@name = @attributes.name
@extra = @attributes.extra
@trace parse: (resp) ->
debug.info 'resp: ' + JSON.stringify resp
@id = resp.id
@attributes.id = @id
@name = resp.name
@attributes.name = @name
@extra = resp.extra
@attributes.extra = @extra
@
注释掉:
@extra = resp.extra
@attributes.extra = @extra
导致错误消失,但 CompositeView 甚至不尝试更新。如何使用获取的模型响应在 parse() 完成时更新 CompositeView?
(虽然示例没有显示它,但我必须重写,因为响应不会像 Backbone 想要的那样返回,以防您想知道)。