2012-04-11 39 views
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 

Rabl的:

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 

在最初呈現一切正常顯示。但是,當我更改模型並重新呈現列表項時,模型的發件人哈希變爲空。因此,渲染將不會打印出發件人的名稱。屬性喜歡標題仍然顯示,因爲它們不是嵌套的。

爲什麼嵌套屬性消失?我是否渲染一些中間模型?

+0

現在有同樣的問題。 – kikuchiyo 2012-05-02 20:54:05

+0

你是怎麼想出來的? – rfunduk 2012-05-30 11:34:19

+0

對於後期更新感到抱歉,但不知何故我的問題在幾個月前消失。 @Chris_Bui你想在我的問題下面編輯和添加你的例子嗎?可能更容易讓其他人調試您的問題。 – lulalala 2012-07-30 08:46:09

回答

1

首先要嘗試的是,在您的InboxItemViewrender方法中,console.log(@model, @model.toJSON())。使用網絡檢查器或螢火蟲,展開結果對象並查看它們之間的差異。沒有真正的原因會丟掉字段,所以您需要弄清楚模型中的內容以及toJSON的內容。

1

我們需要看到你的控制器進行確認,但我懷疑的是,當您保存模型,可以得到一個更新的響應,通過比一個貼了不同的模板Rabl的那張。