Skip to content

Commit

Permalink
Merge pull request #15 from mkeemon/feature/customValidations
Browse files Browse the repository at this point in the history
Checking for promises now implemented correctly for handler() method in ...
  • Loading branch information
Duncan Walker committed Apr 2, 2015
2 parents 09d8bc1 + d263949 commit 8fc5af5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
16 changes: 11 additions & 5 deletions addon/mixins/controllers/saving.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default Ember.Mixin.create(

validateAndSave: function() {
var _this = this;
var runCustomValidations = _this.runCustomValidations;
var customValidationsPromise;

var resolve = function() {
Ember.assert(
Expand All @@ -48,10 +48,16 @@ export default Ember.Mixin.create(

/* If there is a custom validations method, resolve it */

if (runCustomValidations && !runCustomValidations.then) {
Ember.assert('runCustomValidations() must return a promise (e.g. return new Ember.RSVP.Promise()).');
} else if (runCustomValidations) {
runCustomValidations().then(resolve, reject);
if (this.runCustomValidations) {
customValidationsPromise = this.runCustomValidations();

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

customValidationsPromise.then(resolve, reject);
} else {

/* Else save with normal ember-validations checks */
Expand Down
12 changes: 9 additions & 3 deletions addon/mixins/views/submitting.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default Ember.Mixin.create({
var controller = this.get('controller');
var methodName = type + 'Handler';
var handler = this[methodName];
var controllerMethod;
var controllerMethod, handlerPromise;

/* If event is submit, controller method is renamed */

Expand All @@ -88,9 +88,15 @@ export default Ember.Mixin.create({
Ember.typeOf(handler) === 'function'
);

/* TODO - need a way to assert whether handler is a promise */
handlerPromise = handler();

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

handlerPromise.then(function() {
controller[type]();
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-easy-form-extensions",
"version": "0.2.7",
"version": "0.2.8",
"description": "Extends Ember EasyForm into the view and controller layers of your Ember CLI app to provide easy event and action handling using mixins and components.",
"directories": {
"doc": "doc",
Expand Down

0 comments on commit 8fc5af5

Please sign in to comment.