Saving relationships on BOTH ends in Ember Data

I am using Ember Data with the RestAdapter and the following models:

Pizzas.Pizza = DS.Model.extend({
  name: DS.attr('string'),
  orders: DS.hasMany('order', { async: true }),
});

Pizzas.Order = DS.Model.extend({
  date: DS.attr('date'),
  pizzas: DS.hasMany('pizza', { async: true }),
});

I create and save the new order as follows:

var pizza1 = an existing pizza with id 1;
var pizza2 = an existing pizza with id 2;

var newOrder = this.store.createRecord('order');
newOrder.set('date', Date());

newOrder.get('pizzas').then(function(pizzas) {

    pizzas.pushObject(pizza1);
    pizzas.pushObject(pizza2);
    newOrder.save();
});

This works well - Ember Data performs POST on a model Orderthat includes pizza identifiers in a relationship field pizzas. However, I expected that after saving, the order ID would be automatically added to the relationship of the orderstwo pizza objects with Ember Data, but this does not seem to be the case. This causes 2 problems:

  • When I ask for all pizza orders, the new order does not appear (since it was never added to the pizza relationship field)
  • (, ) , ( , PUT ids, )

, :

newOrder.save().then(function() {

        pizza1.get('orders').then(function(orders) {

            orders.pushObject(newOrder);
        })
        // same for pizza 2
    } );

Ember, ( ), - ?

+4
1

-11 .

- , . Ember Data , json apis , , , . . "NoSQL", , , , , ..

, , . , , , .

, , json apis, - ( "" ) hasMany, , .

JSONSerializer, , , , RESTSerializer . , , , NP- !. , , promises, , , . (, ).

UPDATE

- , , POST/PUT ( .then), , . , , . EmbeddedRecordsMixin , , .

, , , , . - , /, validFrom/validTo ..

-, attrs, :

// app/serializers/application.js
import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin,{
  mergedProperties: [ 'attrs' ]
});

, :

// app/serializers/contact.js
import AppSerializer from "./application";

export default AppSerializer.extend({
  attrs: {
    names: { embedded: 'always'},
    addresses: { embedded: 'always' },
    emails: { embedded: 'always' },
    phones: { embedded: 'always' },
    urls: { embedded: 'always' }
  }
});
+1

All Articles