This is a follow-up to my last post that showed how to export an ember data object with embedded relationships into one nested JSON structure. This time we’ll take the same JSON and load it back into ember.
Here’s the JSON object we’ll be importing:
{"child": { "name": "Herbert", "toys": [{ "kind": "Robot", "size": { "height": 5, "width": 5, "depth": 10 }, "features": [{ "name": "Beeps" },{ "name": "Remote Control" }] }] } }
This should create a child record, with one toy that has size and a couple features. The model definition looks like this:
App.Child = DS.Model.extend({ name: DS.attr('string'), toys: DS.hasMany('toy', {embedded: 'always'}), }); Ember.Inflector.inflector.irregular("child", "children"); App.Toy = DS.Model.extend({ kind: DS.attr('string'), size: DS.belongsTo('size', {embedded: 'always'}), features: DS.hasMany('feature', {embedded: 'always'}) }); App.Size = DS.Model.extend({ height: DS.attr('number'), width: DS.attr('number'), depth: DS.attr('number') }); App.Feature = DS.Model.extend({ name: DS.attr('string') });
Continue reading Loading JSON with embedded records into Ember Data 1.0.0 beta