0

示例: 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 想要的那样返回,以防您想知道)。

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

项目视图:

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

复合视图

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 回答