0

例: 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 が望むように応答が返されないため、オーバーライドする必要があります)。

4

3 に答える 3

1

jQuery遅延オブジェクトを使用して、コレクション リクエストが完了するまでビューのレンダリングを延期します。

things = new App.Collections.Things()
things.deferred = things.fetch()

things.deferred.done(function() {
  App.Pages.State.Action1.getLayout().content.show new App.CompositeViews.Action1Things(
    collection: things
  )
});

詳細はこちら:コレクションにデータが入力された後にレンダリングを呼び出す Backbone.js

于 2012-08-27T04:12:49.950 に答える
0

ItemView と CompositeView のon 'change'イベントの両方を変更する必要がありました。

ItemView古い:

class App.ItemViews.Action1Thing extends Backbone.Marionette.ItemView
  template: '#template-action1-thing'
  @trace initialize: (options) ->

    @model.on 'change', @render

ItemView新しい:

class App.ItemViews.Action1Thing extends Backbone.Marionette.ItemView
  template: '#template-action1-thing'
  @trace initialize: (options) ->

    @model.on 'change', () ->
      @render

CompositeView古い:

class App.CompositeViews.Action1Things extends Backbone.Marionette.CompositeView

  template: '#template-action1-things'
  collectionEl: 'tbody#things_collection'
  itemView: App.ItemViews.Action1Thing

  @trace initialize: (options) ->

    @collection.on 'change', @render

CompositeView新しい:

class App.CompositeViews.Action1Things extends Backbone.Marionette.CompositeView

  template: '#template-action1-things'
  collectionEl: 'tbody#things_collection'
  itemView: App.ItemViews.Action1Thing

  @trace initialize: (options) ->

    @collection.on 'change', (model) ->
      @.remove model
      @.add model
      @render
于 2012-08-27T07:16:50.850 に答える
-3

以下は間違いなくうまくいくはずです、それは私にとってはうまくいきました...

以下の代わりに

this.collection.fetch();

これを使って

this.collection.fetch(async: false);
于 2013-06-28T15:08:43.010 に答える