ember.js - Ember self-reference and polymorphism -


there user model relates contact user has_many contacts.

than each contact has , belongs many "groups". both user , contact has 1 address.

i read through http://lukegalea.github.io/ember_data_polymorphic_presentation/#/ couple of times, yet still no understand how setup { user | contact } <-> address relationship , contacts <-> groups association on ember side.

right 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({  }); 

here json

// /contacts {     "contacts":[       {         "name":"conrad",       "address_id":"1",       "id":1     },     {         "name":"greyson",       "address_id":"2",       "id":2     },     {         "name":"tommie",       "address_id":"3",       "id":3     }   ] }  // /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     }   ] } // /users {     "users":[       {         "name":"emile",       "address_id":4,       "id":1     }   ] } 

as far understand there no need in polymorphic here, because wrote: "user model relates itself". should set reflexive relation contacts user model.

when want define reflexive relation, must either explicitly define other side, , set explicit inverse accordingly, , if don't need other side, set inverse 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'd user , contact different ember models 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}), }); 

note: proper paylod addresses should :

// /addresses {"addresses":[     {        "id":1,     "city":"west lillaton",     "profile": {"id:"1", "type":"contact"},   },{        "id":2,     "city":"west lillaton",     "profile": {"id:"2", "type":"user"},   } ]} 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -