Ember自引用和多态性 [英] Ember self-reference and polymorphism

查看:92
本文介绍了Ember自引用和多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个用户模型也与自己相关联,所以用户has_many联系人

there is a user model which also relates to itself as contact so user has_many contacts.

联系人拥有并属于许多团体。用户和联系人都有一个地址。

Than each contact has and belongs to many "groups". Both user and contact has one address.

我阅读了 http:// lukegalea。 github.io/ember_data_polymorphic_presentation/#/ 几次,但还是不明白如何设置 {user |联系人}< - >地址关系和联系人< - >

I read through http://lukegalea.github.io/ember_data_polymorphic_presentation/#/ couple of times, but yet still do no understand how to setup { user | contact } <-> address relationship and a contacts <-> groups association on the Ember side.

现在我有(简化的表示):

Right now I have (a simplified representation):

// Address
export default DS.Model.extend({
  city: DS.attr('string'),
  profile : DS.belongsTo('profile', { polymorphic: true, async: true })
});

// Profile
export default DS.Model.extend({
  name: DS.attr('string'),
  phone: DS.attr('string'),
  address: DS.belongsTo('address', {async: true})
});

// Contact
export default Profile.extend({
  groups: DS.hasMany('group')
});

// User
export default Profile.extend({

});

这里是JSON

// GET /contacts
{  
  "contacts":[  
    {  
      "name":"Conrad",
      "address_id":"1",
      "id":1
    },
    {  
      "name":"Greyson",
      "address_id":"2",
      "id":2
    },
    {  
      "name":"Tommie",
      "address_id":"3",
      "id":3
    }
  ]
}

// GET /addresses
{  
  "addresses":[  
    {  
      "city":"West Lillaton",
      "profile_id":"0",
      "profile_type":"Contact",
      "id":1
    },
    {  
      "city":"Weissnatborough",
      "profile_id":"1",
      "profile_type":"Contact",
      "id":2
    },
    {  
      "city":"Predovicton",
      "profile_id":"2",
      "profile_type":"Contact",
      "id":3
    },
    {  
      "city":"VKHA",
      "profile_id":1,
      "profile_type":"User",
      "id":4
    }
  ]
}
// GET /users
{  
  "users":[  
    {  
      "name":"Emile",
      "address_id":4,
      "id":1
    }
  ]
}


推荐答案

据我所知,在这里不需要多态,因为你写道:用户模型也涉及本身。您应该为用户模型设置反身关系 联系人

As far as I understand there is no need in polymorphic here, because you wrote: "user model which also relates to itself". You should set reflexive relation contacts for user model.

当您要定义反身关系时,您必须明确定义另一方,并相应地设置显式反转,如果不需要另一方,将反向设置为null。

When you want to define a reflexive relation, you must either explicitly define the other side, and set the explicit inverse accordingly, and if you don't need the other side, set the inverse to null.

http://guides.emberjs.com/v1.12.0/models/defining-models/#toc_reflexive-relation

// User
export default DS.Model.extend({
  name: DS.attr('string'),
  phone: DS.attr('string'),
  address: DS.belongsTo('address', {async: true, inverse: 'user'})
  groups: DS.hasMany('group', {async: true}),
  contacts: DS.hasMany('user', { inverse: null }),
});

// Address
export default DS.Model.extend({
  city: DS.attr('string'),
  user: DS.belongsTo('user', { async: true, inverse: 'address' })
});

// Group
export default DS.Model.extend({
  name: DS.attr('string'),
  users: DS.hasMany('user', {async: true}),
});

如果您想要用户联系人成为不同的ember模型,然后
address belongsTo polymorphic 个人资料

If you'd like user and contact to be different ember models then address belongsTo polymorphic profile:

// Profile
export default DS.Model.extend({
  name: DS.attr('string'),
  phone: DS.attr('string'),
  address: DS.belongsTo('address', {async: true, inverse: 'profile'})
});

// Contact
export default Profile.extend({
  groups: DS.hasMany('group', {async: true}),
  user: DS.belongsTo('user', { async: true, inverse: 'contacts' })
});

// User
export default Profile.extend({
  contacts: DS.hasMany('contact', { inverse: 'user' }),
});

// Address
export default DS.Model.extend({
  city: DS.attr('string'),
  profile: DS.belongsTo('profile', { polymorphic: true, async: true, inverse: 'address' })
});

// Group
export default DS.Model.extend({
  name: DS.attr('string'),
  contacts: DS.hasMany('contact', {async: true}),
});

注意:GET地址的正确paylod应该是:

Note: proper paylod for GET addresses should be :

// GET /addresses
{"addresses":[  
  {   
    "id":1,
    "city":"West Lillaton",
    "profile": {"id:"1", "type":"Contact"},
  },{   
    "id":2,
    "city":"West Lillaton",
    "profile": {"id:"2", "type":"User"},
  }
]}

这篇关于Ember自引用和多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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