user1599468
user1599468

Reputation: 73

Load but don't send embedded objects

Say you have the following model:

App.Item = DS.Model.extend({
    owner: DS.belongsTo('App.Person', {embedded: true})
})

This means you can load this embedded association, but it also means that if you want to create a new Item for a person that already exists ember-data will also embed the Person object for every new item.

Is it possible to make it load embedded objects but when creating associations only send the ids? i.e send this instead:

{"item": {"owner_id": 5}}

Edit: To clarify, I want ember-data to load embedded relations, but if I set {embedded: true} this code:

App.Item.createRecord({name: 'Something', owner: App.Person.find(1)});
// And a few moments later when App.Person.find(1) has loaded
App.store.commit()

It will send the following json:

{ "item": {"name": "Something", owner: { id: 1, name: "whatever" }}

But what I want is:

{ "item": {"name": "Something", owner_id: 1 }}

Basically if I set embedded = true ember-data will also embed the assocations when you create an object.

Upvotes: 0

Views: 473

Answers (1)

Mike Aski
Mike Aski

Reputation: 9236

If I correctly understand your aim, you shouldn't have to specify { embedded: true }. The default ember-data behavior is to be lazy.

It you are using active_model_serializers (which I strongly recommend to you), you should declare your server-side serializer as follow:

class ItemSerializer < ActiveModel::Serializer
  embed :ids, include: false
  #...
end

Upvotes: 1

Related Questions