Model Nested Object

Hello,

I’ve tried almost anything to get this to work, but I think I’m missing something somewhere.

I’m using Ember 1.3

My JSON looks somehow like this:

{notes: [{name: "Jamal", seller: {name: "John"}}, {name: "Jamal", seller: {name: "John"}}]} etc.

How do I define seller in the model?

App.Note = DS.Model.extend({
  name: DS.attr('string'),
  seller: DS.belongsTo('App.Seller', {embedded: 'always'})
});

App.Seller = DS.Model.extend({
  name: DS.attr('string')
})

This doesn’t work.

<td>{{note.name}}</td> Works
<td>{{note.seller.name}}</td> Nothing!

It’s my understanding they are dropping support for parsing embedded data within a payload.

Explicit support for embedded records is gone for now.

See: https://github.com/emberjs/data/blob/master/TRANSITION.md#embedded-records

After looking through the source code, DS.EmbeddedRecordsMixin is still defined but not used in any of the default serializers.

Even if it was possible to support embedded records by manually adding the mixin to your serializer. I think a better solution would be to just format your JSON like below:

{
	"notes": [
		{
			id: 1
			name: "Bob",
			seller: 1
		},
		{
			id: 2,
			name: "Jack",
			seller: 2
		}
	],

	sellers: [
		{
			id: 1,
			name: "John"
		},
		{
			id: 2,
			name: "Jane"
		}
	]
}

As long as your payload is returning ids you should be able to override extractArraywithin your NotesSerializer

@jamalsoueidan the ActiveModelAdapter package in Ember Data has a mixin: DS.EmbeddedRecordsMixin which supports embedded records. I’ve forked that to complete the support for ActiveModel::Serializers gem in a separate repo: https://github.com/pixelhandler/ember-data-extensions perhaps read the api docs for the mixin on the ember website and if you need the added support for belongsTo which it looks like you do, then try my fork.

Well there will ONLY be one seller all the time, why add a array :S

How can I access the model RAW json data?

@jamalsoueidan given a model instance (record) you can use record.serialize() to get the JSON that will be communicated over HTTP for CREATE, PUT etc. In an Ember app you don’t addess the model instance’s raw JSON. record.get(‘thing.relatedThing.prop’) is get a property of a related record. See the website guides for how to setup the model relationships, for example a one-to-one needs a property on each model that uses DS.belongsTo(‘resouce_singular_name’). Also see the website repository for the very latest docs: https://github.com/emberjs/website/tree/master/source/guides/models The API docs have info on the DS.EmbeddedRecordMixin.

Not working

{{note.get('note.seller.name')}}

I already tried that, not working, and the guide on the website is for 1.0.0 and I’m using 1.3.

github.com/emberjs/website/blob/master/source/guides/models/index.md

This is still for old website, and I can’t find the guide for 1.3

Example this doesn’t work anymore as the guide URL says?

model: function() {
    return this.store.find('person', 1);
  }

I’m using this

App.NotesRoute = Ember.Route.extend({
  model: function() {
    return App.Note.find();
  }
});

Yeah I know the website guides and docs have been lacking over the past few months, but recently they’ve been improving and In my opinion are now very helpful for getting up and running with Ember Data. I’m using v1.0.0-beta.5 and recently collected notes from the guides on the website.

As for how the models should define relationships and how to lookup records … it helped me to make a jsbin of a simple example of using the belongsTo and hasMany relationships as well as rendering in a handlebars template, see: http://jsbin.com/iCIXuzI/6/edit?html,js,output

Here are my notes on what I think is a list of the fundamentals for using Ember Data: Pixelhandler's blog

Prior to all the updates in the website guides what helped me the most was the transition guide in the ember data repo: https://github.com/emberjs/data/blob/master/TRANSITION.md

@jamalsoueidan Our setup is Django-Tastypie on the backend (so HATEOAS, not the RoR conventions ED expects). I added embedded=true as an option on a relationship and embeddedCommit=true as an option on our adapter.

When pulling down I always expect that records could be embedded (because they often are in the case of child belongsTo, but you might also get an array of resource_uri’s). During PUT/POST I used the options to determine whether or not to embedded records.

I moved away from making custom changes, and just followed the way ember wants the data.

The main point of ember-data 1.0 was that it was expected that custom adapters / serializers would be created since there isn’t a “one size fits all approach”. While the default RESTAdapter and RESTSerializer make a lot of assumptions, extending them you can very quickly get something that matches your system without altering ED at all.

(Link for reference Stabilizing Ember Data )

Thanks I’ll read it .