Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete Ember Data record by its ID

With Ember Data, I would like to know how to delete a record given that I know its id.

like image 330
HaoQi Li Avatar asked Sep 10 '13 01:09

HaoQi Li


People also ask

What is store in Ember?

One way to think about the store is as a cache of all of the records that have been loaded by your application. If a route or a controller in your app asks for a record, the store can return it immediately if it is in the cache.

Is Ember a backend?

Ember does not provide tools for the backend at all, it's meant to be front-end framework only.

What is service in Ember?

A Service is an Ember object that lives for the duration of the application, and can be made available in different parts of your application. Services are useful for features that require shared state or persistent connections. Example uses of services might include: User/session authentication. Geolocation.


1 Answers

Note that after calling rec.deleteRecord() you also need to call rec.save() to "commit" the deletion.

this.get('store').find('model', the_id_of_the_record).then(function(rec){
  rec.deleteRecord();
  rec.save();
});

You can see that this is necessary by adding a record in the JSBin above (http://jsbin.com/iwiruw/458/edit), deleting it, and then reloading the page. You'll see that the record is "resurrected" after the page reloads (or if you click the "Run with JS" button). Here's a jsbin with rec.save() added, where you can see that the records do not come back to life.

http://jsbin.com/iwiruw/460/edit

like image 67
Jeremy Green Avatar answered Nov 10 '22 15:11

Jeremy Green