Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating multiple objects with ember-data and non-rest action

Lets say I have a Customer who hasMany products

The api has a PUT: /customers/:id/cancel that cancels the customer and also cancels all the products and then returns the customer and the products for sideloading:

{
  customer: {
    id: 1,
    name: "Customer Name",
    canceled: true,
    products: [1, 2] },

  products: [
    {id: 1, customer_id: 1, name: "Product 1", canceled: true},
    {id: 2, customer_id: 1, name: "Product 2", canceled: true}
  ]
}

how would I:

  1. Call that action on the customer?
  2. Update all objects in the Ember Store?
like image 488
Kim Fransman Avatar asked Nov 13 '22 15:11

Kim Fransman


1 Answers

For part 1. of your question,

  • You'll want to find it in the store var customer = store.find('customer', 1);
  • Then tell it to delete customer.destroyRecord();
  • And finally commit the delete customer.save();

For part 2. deleting the products, you'll have to do delete them manually in a forEach loop, tucked inside an Ember.run.once., as there is no cascading delete in ember data at this stage of the game.

Here is an example of this for a top level delete, so you would need to follow the same logic but for your products and follow it up with a .then that deletes your connected customer.

  • Single tier Example: http://jsbin.com/samegiqe/1/edit?js,output
  • Connected StackOverflow for further reading: How to delete all records associated with an ember model without clearing local Storage?

  • Complex mixin to solve this: http://jsbin.com/hupabovo/1/edit

  • Its connected StackOverflow: Delete associated model with ember-data

Hope that gets at what you were asking. Cheers!

like image 107
Eric D. Johnson Avatar answered Dec 27 '22 00:12

Eric D. Johnson