【问题标题】:Ember recursive parent-child relationship errorsEmber 递归父子关系错误
【发布时间】:2014-05-20 19:33:17
【问题描述】:

我有一个 Rails 后端,我正在尝试为其创建一个 Ember.js 管理面板。基本上有些类别可以是其他类别的父类别,类别可以有很多“贴纸”,但“贴纸”也可以有很多类别。请考虑在此不可变的后端结构(我可以摆弄序列化程序,仅此而已)。这是我的序列化程序:

类别模型:

class Category < ActiveRecord::Base
  has_many :parent_cats, class_name: "CatToCat",foreign_key: "child_id"
  has_many :children_cats, class_name: "CatToCat",foreign_key: "parent_id"

  has_many :parents, through: :parent_cats
  has_many :children, through: :children_cats
end

类别到类别模型:

class CatToCat < ActiveRecord::Base
  belongs_to :parent, class_name: "Category"
  belongs_to :child, class_name: "Category"
end

贴纸:

class StickerSerializer < ActiveModel::Serializer
  embed :ids, include: true
  attributes :id, :text, :audio, :created_at, :image, :smallImage, :updated_at, :stickerServerID
    has_many :categories
end

类别:

class CategorySerializer < ActiveModel::Serializer
  embed :ids, include: true
  attributes :id, :name, :catImage, :created_at, :catOrder, :catServerID, :updated_at
  has_many :stickers
  has_many :children
  has_many :parents
end

这里是 Ember 模型:

贴纸:

App.Sticker = DS.Model.extend({
    audio: DS.attr("string"),
    image: DS.attr("string"),
    smallImage: DS.attr("string"),
    stickerServerID: DS.attr("string"),
    text: DS.attr("string"),
    updated_at: DS.attr("date"),
    created_at: DS.attr("date"),

    categories: DS.hasMany("category")
});

类别:

App.Category = DS.Model.extend({
    name: DS.attr("string"),
    catImage: DS.attr("string"),
    catOrder: DS.attr("string"),
    catServerID: DS.attr("string"),
    updated_at: DS.attr("date"),
    created_at: DS.attr("date"),

    stickers: DS.hasMany("sticker"),

    children: DS.hasMany("category",{inverse:'parents'}),
    parents: DS.hasMany("category",{inverse:'children'})
});

商店:

App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
  keyForAttribute: function(attr){
    console.log("keyForAttribute:"+attr+"->"+this._super(attr));
  if (attr == "smallImage" || attr == "stickerServerID" || attr == "catOrder" || attr == "catImage" || attr == "catServerID"){
    console.log("try:"+attr);
    return attr;
  }else{
    return this._super(attr);
  }
});

App.ApplicationStore = DS.Store.extend({
  adapter: '-active-model'
});

Chrome 报告的错误:

加载路线时出错:错误:找不到“孩子”的模型

Firefox 报告的错误没有用 :-/

很高兴添加任何其他有用的信息!提前致谢!

【问题讨论】:

  • 在 ember 中设置祖先时遇到同样的问题:stackoverflow.com/questions/24474369/… 我得到了各种建议,但不确定什么应该是最佳实践并且没有让它正常工作。
  • 如果您确实找到了有效的方法,请告诉我^^;
  • 我发现最接近工作的是stackoverflow.com/questions/13727512/self-join-with-ember-data,但我使用它只会在页面加载时为我提供正确的数据,当我链接到其他任何内容时它不会重新加载数据。我可以使用reload(),但这似乎很糟糕。
  • 遇到同样的问题,我想问题出在 Rails 上,因为它在 JSON 输出中将子项呈现在一个单独的数组中,而应该将其呈现在与其父项相同的数组中; categories 数组。
  • 哦,然后连接到 ember 端的父母/孩子?

标签: ruby-on-rails ember.js ember-data relationship


【解决方案1】:

有办法做到这一点,但并不完美。

  • 将child_ids 添加到序列化程序中的attributes。
  • 在 Ember 的模型中将 children 设置为 DS.hasMany。

导轨:

# app/serializers/category_serializer.rb    
class CategorySerializer < ActiveModel::Serializer
  embed :ids, :include => true
  attributes :id, :name, :child_ids
end

Ember (EmberCLI):

// app/models/category.js
import DS from 'ember-data';

export default DS.Model.extend({
  children: DS.hasMany('category'),
  name: DS.attr('string'),
});

问题是序列化程序不会自动加载子项,因此您需要始终一次加载所有类别。

【讨论】:

  • 这就是我的孩子/父母在模型中的设置方式: has_many :parent_cats, class_name: "CatToCat",foreign_key: "child_id" has_many :children_cats, class_name: "CatToCat",foreign_key: "parent_id" 我应该在属性中添加什么?
  • 你为什么指定CatToCat作为类?你在使用 HABTM 关系吗?如果你不这样做,你应该使用Category 作为class_name。为了回答您的问题,您使用 :children_cats 作为关系名称,因此 Rails 创建了一个 :children_cat_ids 方法,您应该在序列化程序中将其添加到 attributes。
  • 我们正在使用 HABTH 并将其映射到带有上述加号的 Rails 中: has_many :parents, through: :parent_cats has_many :children, through: :children_cats 但是你说的只是 ids,所以它会是:children_cat_ids?
  • 当您使用 HABTM 时,它会起作用,但您不会拥有 CatToCat 模型的信息。让我知道结果如何。
  • 两个完整模型都添加到原始帖子中!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多