Persisting Records Edit Page
Records in Ember Data are persisted on a per-instance basis.
Call save()
on any instance of DS.Model
and it will make a network request.
Here are a few examples:
1 2 3 4 5 6 |
var post = store.createRecord('post', { title: 'Rails is Omakase', body: 'Lorem ipsum' }); post.save(); // => POST to '/posts' |
1 2 3 4 5 6 7 |
var post = store.find('post', 1); post.get('title') // => "Rails is Omakase" post.set('title', 'A new post'); post.save(); // => PUT to '/posts/1' |
Promises
save()
returns a promise, so it is extremely easy to handle success and failure scenarios.
Here's a common pattern:
1 2 3 4 5 6 7 8 9 10 11 |
var post = store.createRecord('post', { title: 'Rails is Omakase', body: 'Lorem ipsum' }); post.save().then(function(post) { this.transitionToRoute('posts/show', post); }); // => POST to '/posts' // => transitioning to posts.show route |
Promises even make it easy to work with failed network requests:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
var post = store.createRecord('post', { title: 'Rails is Omakase', body: 'Lorem ipsum' }); var onSuccess = function(post) { this.transitionToRoute('posts/show', post); }; var onFail = function(post) { // deal with the failure here }; post.save().then(onSuccess, onFail); // => POST to '/posts' // => transitioning to posts.show route |
You can read more about promises here, but here is another example showing how to retry persisting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function retry(callback, nTimes) { // if the promise fails return callback().fail(function(reason) { // if we haven't hit the retry limit if (nTimes-- > 0) { // retry again with the result of calling the retry callback // and the new retry limit return retry(callback, nTimes); } // otherwise, if we hit the retry limit, rethrow the error throw reason; }); } // try to save the post up to 5 times retry(function() { return post.save(); }, 5); |