ember.js - Polymorphic relations in embedded relationships -
i have ember cli project , trying replicate simple scenario in have post model hasmany comment model. relationship polymorphic. have 2 type body comment , title comment.
// app/models/post.js import ds 'ember-data'; export default ds.model.extend({ entry: ds.attr('string'), comments: ds.hasmany('comment', {polymorphic: true}) }); // app/models/comment.js import ds 'ember-data'; export default ds.model.extend({ text: ds.attr('string'), post: ds.belongsto('post') }); // app/models/body.js import ds 'ember-data'; import comment './comment'; export default comment.extend({ body: ds.attr('string') }); // app/models/title.js import ds 'ember-data'; import comment './comment'; export default comment.extend({ title: ds.attr('string') });
i have serializer post model
import ds 'ember-data'; export default ds.activemodelserializer.extend(ds.embeddedrecordsmixin, { attrs: { comments: { embedded: 'always' } } });
json returned server on /posts/1 is
{ "posts": { "id": "1", "entry": "this first post", "comments": [ { "id": "1", "post": "1", "type": "body", "text": "this first comment on first post", "body": "this body comment" }, { "id": "2", "post": "1", "type": "title", "text": "this second comment on first post", "title": "this title comment" } ] } }
but ember data failing in deserializing comments following error:
error while processing route: index cannot read property 'typekey' of undefined typeerror: cannot read property 'typekey' of undefined @ ember.object.extend.modelfor (http://localhost:4200/assets/vendor.js:71619:22) @ ember.object.extend.recordforid (http://localhost:4200/assets/vendor.js:71074:25) @ deserializerecordid (http://localhost:4200/assets/vendor.js:72099:27) @ deserializerecordids (http://localhost:4200/assets/vendor.js:72116:9) @ http://localhost:4200/assets/vendor.js:72081:11 @ http://localhost:4200/assets/vendor.js:70135:20 @ http://localhost:4200/assets/vendor.js:17687:20 @ object.orderedset.foreach (http://localhost:4200/assets/vendor.js:17530:14) @ object.map.foreach (http://localhost:4200/assets/vendor.js:17685:14) @ function.model.reopenclass.eachrelationship (http://localhost:4200/assets/vendor.js:70134:42)
this happens when following code executed:
import ember 'ember'; export default ember.route.extend({ model: function() { return this.store.find('post', 1); }, setupcontroller: function(controller, model) { console.log("post entry: " + model.get('entry')); var comments = model.get('comments'); comments.foreach(function(comment){ console.log("comment: " + comment.get('text')); console.log(typeof comment); //console.log("comment body " + comment.get('body')); //console.log("comment title " + comment.get('title')); }); } });
please me in understanding if doing wrong , if yes correct way solve requirement this.
so @dipayan aren't doing wrong here, ember data doesn't yet support hasmany embedded polymorphic relationships yet. polymorphic embedded hasmany relationship. give typeerror: cannot read property 'typekey' of undefined
if try use it, precisely you're getting.
if have control of how server handles data can keep polymorphic, lose embedded, , instead use side-loading. example , explanation of using polymorphic in way check out topal's guide ember data.
Comments
Post a Comment