例: http://franklovecchio-playback.herokuapp.com/?log=true
完全なコード: https://github.com/franklovecchio/playback
「未定義」の属性がエラーで表示されます: Uncaught TypeError: Object #<Thing> has no method 'serializeData'
after I return the model on parse override.
最初に、コレクションを 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 は更新を試みません。CompositeView を parse() 完了時に取得したモデル応答で更新するにはどうすればよいですか?
(例には示されていませんが、Backbone が望むように応答が返されないため、オーバーライドする必要があります)。