ember.js - Ember 2 simple polymorphic relations -
i have notes model want attach 1 of 2 other models, customers , suppliers.
in database have foreigntype , foreignid field holds type , corresponding id customer or supplier,
notes: { {id: 1, body:'bar',foreigntype:'customer',foreignid:100}, {id: 2, body:'foo',foreigntype:'supplier',foreignid:100} }
that is, note can attached customer or supplier.
the convention seems field called notetype? have seen tutorial related type nested in json, rather being @ root.
my ember models this:
//pods/note/model.js export default ds.model.extend({ //... body: ds.attr('string'), foreign: ds.belongsto('noteable',{polymorphic:true}) }); //pods/noteable/model.js (is there better/conventional place put file?) export default ds.model.extend({ notes: ds.hasmany('note') }); //pods/customer/model.js import noteable '../noteable/model'; export default noteable.extend({ //derived noteable class name: ds.attr('string'), //... }); //pods/supplier/model.js // similar customer // sample incoming json // {"customer":{"id":2,"name":"foobar inc",...}, "contacts": [{"id":1757,"foreigntype": "customer","foreignid":2,...}, {"id":1753,"foreigntype": "customer","foreignid":2,...}, ...], ... "todos": [{"id":1,"foreigntype":"customer","foreignid":2,"description":"test todo"}], "notes": [{"id":1,"foreigntype":"customer","foreignid":2,"body":"some customer note "}] }
how set correctly, i.e. ember expect?
my notes aren't attaching correctly customer model. show in data tab of ember inspector, notes list of customer empty.
i can see several possibilities:
extend customer/supplier ds.model , have property
notes: belongsto('noteable')
, mean belongsto in notes isn't polymorphic, there wouldn't derived classes, noteable itself. not sure if ember (data) can deal nesting correctly.extend noteable. if want have other things addresses or contacts, can related customer or supplier?
create duplicate models customernote/suppliernote, customercontact/ suppliercontact, customer/supplier/employee address. , have backend return filtered table/model name depending on endpoint. don't repeat myself though ....
ember : 2.2.0
ember data : 2.2.1
Comments
Post a Comment