Ember自我引用和多态性

时间:2015-06-14 14:10:47

标签: ember.js ember-data

有一个用户模型也将自身与联系user has_many contacts联系起来。

比每个联系人都拥有并且属于许多“组”。用户和联系人都有一个地址。

我通过http://lukegalea.github.io/ember_data_polymorphic_presentation/#/几次阅读,但仍然不明白如何在Ember方面设置{ user | contact } <-> address关系和contacts <-> groups关联。

现在我有(简化表示):

// 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
    }
  ]
}

1 个答案:

答案 0 :(得分:4)

据我所知,这里不需要多态,因为你写道:“用户模型也与本身相关”。您应为contacts模型设置自反关系 user

如果要定义自反关系,则必须明确定义另一侧,并相应地设置显式逆,如果不需要另一侧,则将逆设置为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}),
});

如果您希望usercontact成为不同的余烬模型,那么 address所属多态 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应为:

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