I am creating an app using Ember App Kit with Ember Data. I have an items resource that has both a create and edit form. For various design reasons I do not want to have the model updated or created till the user hits a save button. I am not sure the best deign pattern for doing this in Ember since the default is to bind the form fields to the model which updates it as data is input.
1
votes
2 Answers
2
votes
You need to create a temporary property on your controller.
App.FooController = Em.ObjectController.extend({
userName: '',
createData: function() {
this.get('store').createRecord({
name: this.get('userName')
}).save();
this.set('userName', '');
},
saveData: function() {
this.set('model.name', this.get('userName'));
this.set('userName', '');
}
});
Bind to the userName
property for the text field (or whatever), then transfer the value over when the user clicks the save button.
rollback
method on the models that allow you to rollback unsaved model data. Using theisDirty
property on the model you can check onwillTransition
to see if the model has changes, confirm with the user they want to discard those changes, and if so roll them back using the built in method. Very clean and drops the required code by a lot. – tmartineau