Skip to content

Commit

Permalink
Event handler doing formSubmitted
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncan Graham Walker committed Mar 3, 2016
1 parent 87e09cf commit 87156c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
2 changes: 1 addition & 1 deletion addon/components/form-submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default Ember.Component.extend(
},

delete() {
this.sendAction('delete');
this.sendAction('deleteAction');
},

save() {
Expand Down
32 changes: 13 additions & 19 deletions addon/mixins/controllers/form.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember';
import EmberValidations from 'ember-validations';

const { computed } = Ember;
const { assert, computed } = Ember;

export default Ember.Mixin.create(
EmberValidations, {
Expand All @@ -21,33 +21,23 @@ export default Ember.Mixin.create(
/* Form submission actions */

cancel() {
this.set('formIsSubmitted', true);
this._eventHandler('cancel');
},

delete() {
this.set('formIsSubmitted', true);
this._eventHandler('delete');
},

/* Show validation errors on submit click */

save() {
this.set('formIsSubmitted', true);
this._eventHandler('save');
},

},

/* Methods */

// resetForm: on('routeDidTransition', function() {
// this.resetSubmission();

// /* Add logic for resetting anything to do with
// input here */
// }),

resetSubmission() {
this.set('formIsSubmitted', false);
},
Expand All @@ -56,7 +46,7 @@ export default Ember.Mixin.create(
const _this = this;

function resolve() {
Ember.assert(
assert(
'You need to specify a save method on this controller',
typeof _this.save === 'function'
);
Expand All @@ -65,7 +55,7 @@ export default Ember.Mixin.create(
}

function reject() {
_this.set('formIsSubmitted', false);
_this.resetSubmission();
}

/* If there is a custom validations method, resolve it */
Expand All @@ -74,7 +64,7 @@ export default Ember.Mixin.create(
const customValidationsPromise = this.runCustomValidations();

if (!customValidationsPromise || !customValidationsPromise.then) {
Ember.assert(
assert(
'runCustomValidations() must return a promise (e.g. return new Ember.RSVP.Promise(...)).'
);
}
Expand Down Expand Up @@ -104,6 +94,10 @@ export default Ember.Mixin.create(
return Ember.typeOf(key) === 'function';
}

/* Set the form to be submitted */

this.set('formIsSubmitted', true);

/* If event is save, method is renamed */

if (type === 'save') {
Expand All @@ -112,23 +106,23 @@ export default Ember.Mixin.create(

const method = this[type];

Ember.assert(`You need to specify a ${type} method on this controller`, method && isFunction(method));
assert(`You need to specify a ${type} method on this controller`, method && isFunction(method));

/* If handler exists, resolve the promise then call
the method... */

if (handler) {
Ember.assert(`${handlerMethodName}() must be a function`, isFunction(handler));
assert(`${handlerMethodName}() must be a function`, isFunction(handler));

const handlerPromise = handler();

if (!handlerPromise.then) {
Ember.assert('handler() must return a promise (e.g. return new Ember.RSVP.Promise(...))');
assert('handler() must return a promise (e.g. return new Ember.RSVP.Promise(...))');
}

handlerPromise.then(function() {
handlerPromise.then(() => {
this[type]();
}.bind(this));
});

/* ...Else, just call the method */

Expand Down

0 comments on commit 87156c8

Please sign in to comment.