Ember-Data递归具有多个关联 [英] Ember-Data recursive hasMany association

查看:89
本文介绍了Ember-Data递归具有多个关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人使用ember-data来建模数据树?



我会认为它会是这样的:

  Node = DS.Model.extend({
children:DS.hasMany(Node),
parent:DS.belongsTo(Node)
});

但是,我无法得到这样的工作,导致以下两点之一:1)我是如何设置这一点的,但是我错了,或者2)目前不可能使用ember-data来建模树。



我是希望它是前者而不是后者...



当然可以是JSON ...我假设JSON应该是这样的形式: / p>

  {
nodes:[
{id:1,children_ids:[2,3],parent_id: null},
{id:2,children_ids:[],parent_id:1},
{id:3,children_ids:[],parent_id:1}
]
}

对此问题的任何提示/建议将不胜感激。

解决方案

有几件小事妨碍你的小提琴工作:




  • DS.hasMany 函数要求一个String作为参数。不要忘记引号:<?c $ c>在夹具定义中,DS.hasMany('Node')


  • hasMany 关系不应该被 _ids 或任何东西后缀。只需使用纯名字。例如: {id:42,children:[2,3],parent_id:17}


  • 长度属性 DS.ManyArray 应该使用 get function: root.get('children.length')


  • 默认情况下,灯具适配器模拟ajax调用。 查找查询将在等待50ms后填充记录。在你的小提琴中, root.get('children.length')来电太早了。您可以配置灯具适配器,使其同步调用:

      App.store = DS.Store.create({
    revision:4,
    adapter:DS.FixtureAdapter.create({
    simulateRemoteResponse:false
    })
    });

    或者您可以在没有任何适配器的情况下将数据加载到商店:

      App.store.loadMany(App.Node,[
    {id:1,children:[2,3]},
    {id :2,children:[],parent_id:1},
    {id:3,children:[],parent_id:1}
    ]);


  • 最后一个:似乎Ember应用程序应该在全局范围中声明不需要$ var ),并且应该在应用范围内声明Ember数据模型(替换 var Node = ... 通过 App.Node = ...




示例:

  App = Ember.Application.create(); 

App.store = DS.Store.create({
revision:4
});

App.Node = DS.Model.extend({
children:DS.hasMany('App.Node'),
parent:DS.belongsTo('App.Node ')
});

App.store.loadMany(App.Node,[
{id:1,children:[2,3]},
{id:2,children:[] ,parent_id:1},
{id:3,children:[],parent_id:1}
]);

var root = App.store.find(App.Node,1);

alert(root.get('children'));
alert(root.get('children.length'));


Has anyone used ember-data to model a tree of data?

I would assume it would be something like:

Node = DS.Model.extend({
    children: DS.hasMany(Node),
    parent:   DS.belongsTo(Node)
});

However, I have not been able to get this working which leads be to believe that either: 1) I'm just plain wrong in how I'm setting this up or, 2) it is not currently possible to model a tree using ember-data.

I'm hoping that it's the former and not the latter...

Of course it could be the JSON...I'm assuming the JSON should be of the form:

{
    nodes: [
        { id: 1, children_ids: [2,3], parent_id: null },
        { id: 2, children_ids: [], parent_id: 1 },
        { id: 3, children_ids: [], parent_id: 1 }
    ]
}

Any tips/advice for this problem would be greatly appreciated.

解决方案

There are several little things that prevent your fiddle to work:

  • the DS.hasMany function asks for a String as argument. Don't forget the quotes: DS.hasMany('Node')

  • in the fixture definition, hasMany relationships should not be postfixed by _ids or anything. Just use the plain name. For instance: { id: 42, children: [2,3], parent_id: 17 }

  • the length property of DS.ManyArray should be accessed using the get function: root.get('children.length')

  • by default, the fixture adapter simulates an ajax call. The find query will populate the record after waiting for 50ms. In your fiddle, the root.get('children.length') call comes too early. You can configure the fixture adapter so that it makes synchronous call:

    App.store = DS.Store.create({
        revision: 4,
        adapter: DS.FixtureAdapter.create({
            simulateRemoteResponse: false
        })
    });
    

    Or you can load data to the store without any adapter:

    App.store.loadMany(App.Node, [
        { id: 1, children: [2, 3] },
        { id: 2, children: [], parent_id: 1 },
        { id: 3, children: [], parent_id: 1 }
    ]);
    

  • and last one: it seems like the Ember app should be declared in the global scope (no var), and Ember-data models should be declared in the app scope (replacing var Node = ... by App.Node = ...)

Full example:

App = Ember.Application.create();

App.store = DS.Store.create({
    revision: 4
});

App.Node = DS.Model.extend({
    children: DS.hasMany('App.Node'),
    parent:   DS.belongsTo('App.Node')
});

App.store.loadMany(App.Node, [
    { id: 1, children: [2, 3] },
    { id: 2, children: [], parent_id: 1 },
    { id: 3, children: [], parent_id: 1 }
]);

var root = App.store.find(App.Node, 1);

alert(root.get('children'));
alert(root.get('children.length'));

这篇关于Ember-Data递归具有多个关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆