Ember Data - rollback issue
Ember Data - rollback issue
by cyclomarc
HTML
<script src="http://libraries.azurewebsites.net/js/bootstrap-2.3.2.js"></script>
<script src="http://libraries.azurewebsites.net/js/moment-2.1.0.js"></script>
<link rel="stylesheet" href="http://libraries.azurewebsites.net/css/bootstrap-2.3.2.css">
<script src="http://libraries.azurewebsites.net/js/handlebars-1.0.0.js"></script>
<script src="http://libraries.azurewebsites.net/js/ember-1.0.0.js"></script>
<script src="http://libraries.azurewebsites.net/js/ember-data-1.0.0-beta.1.js"></script>
<script type="text/x-handlebars" id="application">
<h2>Authors</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" id="authors">
<h3>List</h3>
{{#each author in content}}
{{#linkTo 'authors.edit' author}}{{author.name}}{{/linkTo}}<br />
{{/each}}
<br />
{{outlet}}
</script>
<script type="text/x-handlebars" id="authors/edit">
<h3>Details</h3>
ID: {{id}}<br />
Name: {{view Ember.TextField valueBinding='name'}}<br />
Description: {{view Ember.TextField valueBinding='description'}}<br />
<button {{action 'save'}}>Save changes</button>
<button {{action 'cancel'}}>Revert changes</button>
</script>
JavaScript
App = Ember.Application.create();
App.Router.map(function () {
this.resource("authors", { path: '/' }, function () {
this.route('edit', { path: '/:author_id' });
});
});
App.AuthorsRoute = Ember.Route.extend({
model: function () {
return this.store.find('author');
}
});
App.AuthorsController = Ember.ArrayController.extend({
//
});
App.AuthorsEditRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('author', params.author_id);
}
});
App.AuthorsEditController = Ember.ObjectController.extend({
actions: {
save: function () {
var author = this.get('model');
author.save().then(
function () {
alert("Record saved");
},
function (error) {
if (error.status == 422) {
alert("Validation error");
} else {
alert("Other error");
}
}
);
},
cancel: function () {
var author = this.get('model');
author.rollback();
},
}
});
/////FIXTURES DATA
App.Store = DS.Store.extend({
adapter: DS.FixtureAdapter.create()
});
App.Author = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
});
App.Author.FIXTURES =
[
{
id: '124',
name: 'Marc Vermeulen',
description: 'Description comes here'
},
{
id: '125',
name: 'James Coock',
description: 'Description comes here'
}
];