加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 在模型更改重新渲染时,一些嵌套属性消失

发布时间:2020-12-17 07:09:57 所属栏目:百科 来源:网络整理
导读:我有以下消息主干设置: 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 it
我有以下消息主干设置:

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

在初始渲染期间,一切都正常显示.但是,当我更改模型并重新呈现列表项时,模型的发送方哈希变为空.因此,渲染不会打印出发件人的名称.属性喜欢标题仍然显示,因为它们不是嵌套的.

为什么嵌套属性会消失?我在渲染一些中间模型吗?

解决方法

首先要尝试的是,在你的InboxItemView的render方法中,console.log(@model,@ model.toJSON()).使用Web检查器或firebug,展开生成的对象并查看它们之间的区别(如果有的话).没有真正的理由会丢弃字段,因此您需要弄清楚模型中的内容以及toJSON的内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读