7
votes

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

3

3 Answers

10
votes

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

9
votes

In recent versions of Ember Data (beta 4 and newer), destroyRecord() was introduced, which does deleteRecord() and save() in one go, so a shorter way of doing what Jeremey Green proposed is:

this.get('store').find('model', the_id_of_the_record).then(function(rec){
  rec.destroyRecord();;
});
0
votes

Please refer to Jeremy's answer where he adds the critical rec.save()

You can use:

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

or

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

See it in action in this jsbin.