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
ember.js ember-data


source to share


No one has answered this question yet

Check out similar questions:

nine
Ember-Data: how to use `DS.Adapter.findHasMany`
7
Ember.js: integrating test components that interact with ember-data data models
4
Ember-Data beta 3 and retaining hasMany relationships and also one entry with Rails
3
Ember.js Writing Asynchronous Test Helpers and Tests Using Asynchronous Operations
3
Ember Data belongs to asynchronous relationship omitted from createRecord () save () serialization
3
Ember-Data: Adding Server-Side Requests to AJAX Requests
1
Testing a component using the ember-data datastore
0
ember createRecord data without relationship field data
0
how to store an object in ember data when the model consists of more than one object?
0
How to load nested records (master data records) using Ember data?



All Articles
Loading...
X
Show
Funny
Dev
Pics