Google News
logo
Ember.js - Interview Questions
How to Creating, Updating and Deleting Records in Ember.js?
Creating Records : You can create records by calling the createRecord() method on the store.
store.createRecord('post', {
  title: 'Rails is Omakase',
  body: 'Lorem ipsum'
});
The store object is available in controllers and routes using this.store.
 
Updating Records : Making changes to Ember Data records is as simple as setting the attribute you want to change:
this.store.findRecord('post', 1).then(function(post) {
  // ...after the record has loaded
  post.title = 'A new post';
});
 
Deleting Records : Deleting records is as straightforward as creating records. Call deleteRecord() on any instance of Model. This flags the record as isDeleted. The deletion can then be persisted using save(). Alternatively, you can use the destroyRecord method to delete and persist at the same time.
let post = store.peekRecord('post', 1);
post.deleteRecord();
post.isDeleted; // => true
post.save(); // => DELETE to /posts/1

// OR
post = store.peekRecord('post', 2);
post.destroyRecord(); // => DELETE to /posts/2
Advertisement