将Ember.js模型组织为树形结构 [英] Ember.js model to be organised as a tree structure
本文介绍了将Ember.js模型组织为树形结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Rails后端学习Ember.js。我正在寻找一种树结构,以将组模型与其自身(子组)相关联。由于它已经相当成熟,我想将祖传宝石链接起来,以便在Ember端食用。 / p>
祖先将一个名为 ancestry的字符串列添加到我的组模型中,并返回一个父ID字符串。在这种情况下,如何建立Ember模型?
解决方案
我通过修改组序列化程序解决了这个问题
#serializers / group_serializer.rb
class GroupSerializer<和Ember模型。 ActiveModel :: Serializer
属性:id,:name,:parents,:subgroups
def父母
object.ancestor_ids除非object.is_root?
结束
def子组
object.descendant_ids如果object.has_children?
结束
结束
#app / javascripts / models / group.js.coffee
App.Group = DS.Model.extend
名称:DS .attr'字符串'
父母:DS.hasMany'group'
子组:DS.hasMany'group'
I am learning Ember.js, using a Rails backend. I'm looking to set up a tree structure for relating a Group model to itself (Sub-Groups). Since it's pretty mature, I'd like to link up the Ancestry gem for consumption on the Ember side.
Ancestry adds a string column called "ancestry" to my Group model, and returns a string of parent ids. How would one approach the setting up Ember models in this case?
解决方案
I figured it out with some tinkering around with the group serializer and Ember model.
# serializers/group_serializer.rb
class GroupSerializer < ActiveModel::Serializer
attributes :id, :name, :parents, :subgroups
def parents
object.ancestor_ids unless object.is_root?
end
def subgroups
object.descendant_ids if object.has_children?
end
end
# app/javascripts/models/group.js.coffee
App.Group = DS.Model.extend
name: DS.attr 'string'
parents: DS.hasMany 'group'
subgroups: DS.hasMany 'group'
这篇关于将Ember.js模型组织为树形结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文