4

次のメッセージ バックボーンのセットアップがあります。

  class InboxItemView extends Backbone.View
    initialize: ->
      @model.on('change', @render, @)
    render: ->
      @$el.html JST['buy/messages/templates/received_message'](@model.toJSON())
      @

  class InboxListView extends Backbone.View
    items: []
    initialize: ->
      @collection.on('reset', @reset, @)
    reset: ->
      _.each @items, (item) -> item.remove()
      @items = _.map @collection.received(), (model) =>
        item = new InboxItemView(model: model)
        @$('tbody').append item.render().el
        item

モデル

  class Message extends Backbone.Model

  class Messages extends Backbone.Collection
    model: Message
    url: '/messages'
    received: -> @filter (message) -> message.get('receiver').id == gon.userId

ラブル:

object @message
attributes :id, :title, :body, :read_at, :created_at, :last_reply

node :path do |message|
  message_path(message)
end

child :sender => :sender do
  attributes :id, :nickname
end

child :receiver => :receiver do
  attributes :id, :nickname
end

最初のレンダリング中に、すべてが正しく表示されます。ただし、モデルを変更してリスト アイテムを再レンダリングすると、モデルの送信者ハッシュが空になります。したがって、render は送信者の名前を出力しません。title などの属性はネストされていないため、引き続き表示されます。

ネストされた属性が消えるのはなぜですか? 中間モデルをレンダリングしていますか?

4

2 に答える 2

1

最初に試すことは、あなたInboxItemViewrender方法で、console.log(@model, @model.toJSON()). Web インスペクターまたは firebug を使用して、結果のオブジェクトを展開し、それらがどのように異なるかを確認します。フィールドが削除される本当の理由はないので、モデルに何があり、何がモデルから出てくるのかを理解する必要がありtoJSONます。

于 2012-05-28T22:08:48.790 に答える