import Ember from 'ember';

export default Ember.Controller.extend({
  form: function() {
    return this.store.createRecord('ssh-key');
  },

  actions: {
    deleteKey: function(key) {
      this.store.find('ssh-key', key.id).then(function(key) {
        key.destroyRecord();
      });
    },
    save: function(deferred) {
      var self = this;

      // Create a new record based on the values in the form
      var key = this.get('key');
      var name = this.get('name');
      var sshKey = this.store.createRecord('ssh-key', {
        name: name,
        key: key
      });

      // validate the record
      sshKey.validate();

      if (sshKey.get('isValid')) {
        sshKey.save().then(function() {
          // resolve the deferred promise
          deferred.resolve();
          // clear the form
          self.set('key', '');
          self.set('name', '');
        });
      } else {
        deferred.reject();
        sshKey.destroyRecord();
      }
    }
  }
});

@@ -670,50 +670,8 @@
 ) %7B%0A
-        sshKey.save().then(function() %7B%0A  
     
@@ -710,26 +710,24 @@
 ise%0A        
- 
- 
 deferred.res
@@ -733,16 +733,57 @@
 solve();
+%0A%0A        sshKey.save().then(function() %7B
 %0A       

Resolve the promise once we know the model is valid