Search code examples
ember.jsember-dataember-app-kit

Ember - Update model on save not as user types


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.


Solution

  • 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.