1
votes

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.

2
After the fact I have discovered another solution if using Ember Data. I learnt that there is a rollback method on the models that allow you to rollback unsaved model data. Using the isDirty property on the model you can check on willTransition 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

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.

1
votes

Alternatively, you can create an Ember.Object to which you copy the model properties and then bind the form to that object. Then copy the properties back to the model upon save. You can probably save some code this way.