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';
});
Persisting Records
Records in Ember Data are persisted on a per-instance basis.
Call save()
on any instance of Model
and it will make a network request.
Ember Data takes care of tracking the state of each record for you. This allows Ember Data to treat newly created records differently from existing records when saving.
By default, Ember Data will POST
newly created records to their type URL.
let post = store.createRecord('post', {
title: 'Rails is Omakase',
body: 'Lorem ipsum'
});
post.save(); // => POST to '/posts'
Records that already exist on the backend are updated using the HTTP PATCH
verb.
store.findRecord('post', 1).then(function(post) {
post.title; // => "Rails is Omakase"
post.title = 'A new post';
post.save(); // => PATCH to '/posts/1'
});
You can tell if a record has outstanding changes that have not yet been
saved by checking its
hasDirtyAttributes
property. You can also see which parts of
the record were changed and what the original value was using the
changedAttributes()
method. changedAttributes
returns an object, whose keys are the changed
properties and values are an array of values [oldValue, newValue]
.
person.isAdmin; // => false
person.hasDirtyAttributes; // => false
person.isAdmin = true;
person.hasDirtyAttributes; // => true
person.changedAttributes(); // => { isAdmin: [false, true] }
At this point, you can either persist your changes via save()
or you can roll back your changes using rollbackAttributes()
.
person.hasDirtyAttributes; // => true
person.changedAttributes(); // => { isAdmin: [false, true] }
person.rollbackAttributes();
person.hasDirtyAttributes; // => false
person.isAdmin; // => false
person.changedAttributes(); // => {}
Handling Validation Errors
If the backend server returns validation errors after trying to save, they will
be available on the errors
property of your model. Here's how you might display
the errors from saving a blog post in your template:
<div class="error"></div>
<div class="error"></div>
Promises
save()
returns
a promise, which makes it easy to asynchronously handle success and failure
scenarios. Here's a common pattern:
// Assumed to have already injected the router and store services
const newPost = this.store.createRecord('post', {
title: 'Rails is Omakase',
body: 'Lorem ipsum'
});
try {
await newPost.save();
this.router.transitionTo('posts.show', newPost.id);
} catch (error) {
// Handle error
}
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