mercredi 11 novembre 2015

Ember Data: Change belongsTo parent ID sent to the server

Saving this model:

  export default DS.Model.extend({
      type: DS.attr('string'),
      primary: DS.attr('boolean'),
      address: DS.attr('string'),
      address2: DS.attr('string'),
      city: DS.attr('string'),
      state: DS.attr('string'),
      postal: DS.attr('string'),
      customer: DS.belongsTo('customer')
   });

Using this route:

   3 export default Ember.Route.extend({
   4   model() {
   5     return this.store.createRecord('customer-address', {
   6       customer: this.modelFor('customers/show')
   7     });
   8   },
   9   actions: {
  10     save() {
  11       var model = this.modelFor('customer-addresses/new');
  12       model.save().then(() => {
  13         this.transitionTo('customer-addresses');
  14       });
  15     },
  16     cancel() {
  17       this.transitionTo('customer-addresses');
  18     }
  19   }
  20 });

Params are sent to my backend like this:

Parameters: %{"customer_address" => %{"address" => nil, "address2" => nil, "city" => nil, "customer" => "23", "postal" => nil, "primary" => false, "state" => nil, "type" => ""}}

customer needs to be customer_id (a la Rails) as customer hasMany customer_addresses.




Aucun commentaire:

Enregistrer un commentaire