Ember self-regulation and polymorphism

there is a custom model that also treats it as a contact user has_many contacts

.

Than each contact has and belongs to many "groups". The user and the contact have the same address.

I've read through http://lukegalea.github.io/ember_data_polymorphic_presentation/#/ a couple of times, but still don't understand how to set up relationships { user | contact } <-> address

and contacts <-> groups

associations on the Ember side.

I now have (simplified view):

// 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({

});

      

Here is the 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
    }
  ]
}

      

+3


source to share


1 answer


As far as I understand, there is no need for polymorphism here because you wrote: "Custom model, which also applies to ). You must establish a reflective attitude contacts

for the model user

.

If you want to define a reflexive relation, you must either explicitly define the other side and set an explicit inverse accordingly, or if you don't want 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}),
});

      



If you want user

and contact

be different ember models
then address

belongs to 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}),
});

      

Note: correct 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"},
  }
]}

      

+4


source







All Articles