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

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -