In Ember Data, how do I set a relationship field by id without sending NULL to the server?

I have an order model

export default DS.Model.extend({
  account: DS.belongsTo('account', {async: true}),
  address: DS.belongsTo('address', {async: true}),
  orderItems: DS.hasMany('orderItem', {async: true}),
  orderStatus: DS.belongsTo('orderStatus', {async: true}),
  specialInstructions: DS.attr('string'),
  creationDate: DS.attr('date')
});

      

It has an order status that has a model:

export default DS.Model.extend({
  value: DS.attr('string'),
  orders: DS.hasMany('order', {async: true})
});

      

In order / create order I use model

to get the account and then afterModel

to immediately create a new order in the db. I want to create it in afterModel like this:

var order = this.store.createRecord('order', {
  account: model,
  orderStatus: 1
});

return Ember.RSVP.hash({
  order: order.save().then(function() {
    self.set('order', order);
  }),
  products: this.store.findAll('product').then(function(result) {
    self.set('products', result);
  })
});

      

But looking at the POST to the server, Ember is sending orderStatus: null to the server.

In addition to this, I want to set it again later in the saveOrder action (on the controller).

order.set('orderStatus', 3);
order.save().then(function() {
    self.transitionToRoute('member.orders', order.get('account'));
});

      

Can I do it like this? I also have components that want to dispatch actions to persist objects (looking at Ember 2.0) where the id would be perfect as I don't want to access the store in the component etc.

+3


source to share





All Articles