javascript - Creating and updating nested objects in Ember -
i got nested json data server this:
{ name: "alice", profile: { something: "abc" } }
and have following model:
app.user = ember.object.extend({ name: null, profile: ember.object.extend({ something: null }) })
if app.user.create(attrs)
or user.setproperties(attrs)
, profile
object gets overwritten plain js object, i'm doing this:
var profileattr = attrs.profile; delete attrs.profile user.setproperties(attrs); // or user = app.user.create(attrs); user.get('profile').setproperties(profileattrs);
it works, i've got in few places , in real code i've got more 1 nested object, wondering if it's ok override user#create
, user#setproperties
methods automatically. maybe there's better way?
based on comment, want automatic merging behaviour models (the sort of thing .extend()
). in case, try registering custom transformer, like:
app.objecttransform = ds.transform.extend({ deserialize: function(json){ return ember.object.create(json); } }); app.user = ds.model.extend({ profile: ds.attr('object') });
see: https://github.com/emberjs/data/blob/master/transition.md#json-transforms
Comments
Post a Comment