Embedded Record is not getting loaded in Ember.js -
following json data trying load using ember-data:
{ "product" : [ { "id" : 1, "name" : "product1", "master" : { "id" : 1, "name" : "product1", "images" : [ { "id" : 1, "producturl" : "/images/product1_1.jpg" }, { "id" : 2, "producturl" : "/images/product1_2.jpg" } ] } }, { "id" : 2, "name" : "product2", "master" : { "id" : 2, "name" : "product2", "images" : [ { "id" : 3, "producturl" : "/images/product2_1.jpg" }, { "id" : 4, "producturl" : "/images/product2_2.jpg" } ] } } ] }
the models follows:
app.product = ds.model.extend name: ds.attr('string') description: ds.attr('string') master: ds.belongsto('master') app.master = ds.model.extend images: ds.hasmany('image') app.image = ds.model.extend producturl: ds.attr('string')
the application serializer code follows:
app.applicationserializer = ds.activemodelserializer.extend(ds.embeddedrecordsmixin, attrs: { images: { embedded : 'always' } master: { embedded : 'always' } } )
the problem "master" model records being returned empty. not sure, going wrong.
i using following platform configuration:
- ember-source (1.4.0)
- ember-data-source (1.0.0.beta.7)
- ember-rails (0.15.0)
- rails (4.1.0)
thanks
a couple of things jump out @ me immediately. first need finish defining models receive relationships.
app.product = ds.model.extend name: ds.attr('string') description: ds.attr('string') master: ds.belongsto('master') app.master = ds.model.extend images: ds.hasmany('image') products: ds.hasmany('product') app.image = ds.model.extend producturl: ds.attr('string') master: ds.belongsto('master')
emberdata operates opposite of rails when considering relationship id's. id key has exist on parent object opposed child object convention in rails, i'm assuming backend running off of because using active model serializer in ember.
secondly should using model specific serializer data, may have issues down road trying serialize data in other models. ember convention serializer same name model changing serializer code
app.productserializer = ds.activemodelserializer.extend(ds.embeddedrecordsmixin, attrs: { images: { embedded : 'always' } master: { embedded : 'always' } } )
will let ember know want use serializer product model class.
also, when records returning empty, returning empty? in data section of ember inspector? or in payload being send serializer? being asked productroute class? these things point in right direction towards finding problem if isn't solved fixing model relationships.
Comments
Post a Comment