Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Data: Saving a model with an association in one request

I have two ember models with a relationship like this

App.Foo = DS.Model.extend
   bar: DS.belongsTo("App.Bar", embedded: true)

App.Bar = DS.Model.extend
   primaryKey: "blah"
   blah: DS.attr "string

If I create and save a new record like this:

foo = App.store.createRecord App.Foo
foo.set "bar", App.Bar.createRecord(blah: "blahblah")
App.store.commit()

I see 2 post requests to the server:

URL: /foos
Payload: {"foo":{"bar":null}}

and

URL: /bars
Payload: {"bar":{"blah":"blahblah"}}

The association is embedded so I would like to see:

URL: /foos
Payload: {"foo":{"bar":{"blah":"blahblah"}}}

Can I achieve this with the ember-data REST adapter or do I need to write my own code to do this?

like image 358
Charlie Avatar asked Sep 27 '12 17:09

Charlie


People also ask

What are polymorphic models in Ember?

Polymorphism. Polymorphism is a powerful concept which allows a developer to abstract common functionality into a base class. Consider the following example: a user with multiple payment methods. They could have a linked PayPal account, and a couple credit cards on file.

Which of the following Cannot be a reflexive relationship in Ember?

<br> `therefore` 512 cannot be the number of reflexive relations defined on a set A.

What is store in Ember?

Class Store The store contains all of the data for records loaded from the server. It is also responsible for creating instances of Model that wrap the individual data for a record, so that they can be bound to in your Handlebars templates. Define your application's store like this: app/services/store.js.


1 Answers

I am observing the same behavior in my application. Setting 'embedded' to true only helps you get data as embedded, but while you post it separate requests will be generated. You have write your in code if you want to achieve it in one request.

like image 127
inertia Avatar answered Sep 18 '22 11:09

inertia