Creating and Deleting Records Edit Page


You can create records by calling the createRecord method on the store.

1
2
3
4
store.createRecord('post', {
  title: 'Rails is Omakase',
  body: 'Lorem ipsum'
});

The store object is available in controllers and routes using this.store.

Although createRecord is fairly straightforward, the only thing to watch out for is that you cannot assign a promise as a relationship, currently.

For example, if you want to set the author property of a post, this would not work if the user with id isn't already loaded into the store:

1
2
3
4
5
6
7
var store = this.store;

store.createRecord('post', {
  title: 'Rails is Omakase',
  body: 'Lorem ipsum',
  author: store.find('user', 1)
});

However, you can easily set the relationship after the promise has fulfilled:

1
2
3
4
5
6
7
8
9
10
var store = this.store;

var post = store.createRecord('post', {
  title: 'Rails is Omakase',
  body: 'Lorem ipsum'
});

store.find('user', 1).then(function(user) {
  post.set('author', user);
});

Deleting Records

Deleting records is just as straightforward as creating records. Just call deleteRecord() on any instace of DS.Model. This flags the record as isDeleted and thus removes it from all() queries on the store. The deletion can then be persisted using save(). Alternatively, you can use the destroyRecord method to delete and persist at the same time.

1
2
3
4
5
6
7
8
9
10
11
12
13
var post = store.find('post', 1);

post.deleteRecord();

post.get('isDeleted'); // => true

post.save(); // => DELETE to /posts/1

// OR

var post = store.find('post', 2);

post.destroyRecord(); // => DELETE to /posts/2