ember.js - Best Practice for Creating New Record with belongsTo Relationship -
i wondering best practice creating new record in ember createrecord()
, persisting api? specifically, should ember's post
request single json embeds model's relationships, or customary post
each relationship individually?
in code, i'm not able single json, i'm wondering if i'm missing "ember way" or (more likely) have mistake in code?
details:
here details of setup. have 2 models:
/models/orguser.js:
ds.model.extend({ ... orgperson: ds.belongsto('org-person', { inverse: 'org-user', async: true, embedded: 'always' }), });
/models/orgperson.js:
ds.model.extend({ ... orguser: ds.belongsto('org-user'), })
i'm attempting create new user on "create new user" page. route page below. best place call createrecord()
new models?
/routes/org-users/add.js:
ember.route.extend({ model: function() { var orgperson = this.store.createrecord('org-person'); var orguser = this.store.createrecord('org-user' ); orguser.set('orgperson', orgperson); return orguser; }, ... }
using chrome console @ orguser object after call set
shows no evidence @ have added orguser
. "ember" tab of chrome debug tools reflect relationship, though.
on "create new user" page, input fields correspond both orguser
properties , orguser.orgperson
properties. here's example:
/templates/org-users/add.hbs
... {{input value=username}} // property of orguser {{input value=orgperson.firstname}} // property of orguser.orgperson ...
in route, when go save()
ember data post
s orguser json null
value orgperson.
i'd embed orgperson
serialized object in orgperson
property.
/routes/org-users/add.js:
ember.route.extend({ ... actions: { submitform: function() { ... this.currentmodel.save().then( onsuccess ).catch( onfailure ); ... } } });
this results in post
request following body:
{ "orguser":{ "username":"myusername", "orgperson":null, "id":null }
note orgperson
null. in advance assistance!
update: once again, think need take fresh @ serializer. here's how it's defined.
/serializers/application.js:
ds.restserializer.extend({ // use default approach serializing, add id property serialize: function(record, options) { var json = this._super.apply(this, arguments); json.id = record.id; return json; }, serializebelongsto: function(record, json, relationship) { var key = relationship.key; key = this.keyforrelationship ? this.keyforrelationship(key, 'belongsto') : key; var data = record.get('data'); if (relationship.options.embedded && relationship.options.embedded === 'always') { json[key] = data[relationship.key] ? data[relationship.key].serialize( { includeid: true } ) : null; } else { json[key] = data[relationship.key] ? data[relationship.key].get('id') : null; } if (relationship.options.polymorphic) { this.serializepolymorphictype(record, json, relationship); } } });
per @kingpin2k's comment, there appears ambiguity (and bugs!) on how best handle serialize()
belongsto
relationship. serializer customization above works great records obtained through this.store.find()
, need enable them createrecord()
. additional suggestions, pointers welcome!
it's bug. https://github.com/emberjs/data/issues/1542#issuecomment-49443496
a workaround async belongsto record before attempting save (it tricks ember data initializing it). in case in model hook.
model: function() { var orgperson = this.store.createrecord('org-person'); var orguser = this.store.createrecord('org-user'); orguser.set('orgperson', orgperson); return orguser.get('orgperson').then(function(){ return orguser; }); },
Comments
Post a Comment