Permalink
Browse files

Uses promises for form submit

  • Loading branch information...
1 parent 7295c9f commit c51999c89453f1391ffbaf12d416314016fdd506 @bcardarella bcardarella committed Mar 1, 2013
Showing with 47 additions and 28 deletions.
  1. +1 −1 ember-dev.yml
  2. +11 −4 packages/ember-easyForm/lib/views/form.js
  3. +35 −23 packages/ember-easyForm/tests/helpers/formFor_test.js
View
2 ember-dev.yml
@@ -3,6 +3,6 @@ name: Ember EasyForm
assetfile: 'Assetfile'
testing_suites:
default:
- - "package=all&jquery=1.7.2&extendprototypes=true"
+ - "package=all&extendprototypes=true"
testing_packages:
- ember-easyForm
View
15 packages/ember-easyForm/lib/views/form.js
@@ -3,13 +3,20 @@ Ember.EasyForm.Form = Ember.View.extend({
attributeBindings: ['novalidate'],
novalidate: 'novalidate',
submit: function(event) {
- var object = this.get('context').get('content');
+ var object = this.get('context').get('content'), _this = this;
- if (object.validate === undefined || object.validate()) {
- this.get('controller').send('submit');
- }
if (event) {
event.preventDefault();
}
+
+ if (object.validate === undefined) {
+ this.get('controller').send('submit');
+ } else {
+ object.validate().then(function() {
+ if (object.get('isValid') === true) {
+ _this.get('controller').send('submit');
+ }
+ });
+ }
}
});
View
58 packages/ember-easyForm/tests/helpers/formFor_test.js
@@ -5,8 +5,10 @@ var templateFor = function(template) {
var original_lookup = Ember.lookup, lookup;
Model = Ember.Object.extend({
validate: function() {
- return valid;
- },
+ var promise = new Ember.Deferred();
+ promise.resolve();
+ return promise;
+ }
});
module('the formFor helper', {
@@ -60,22 +62,25 @@ test('renders a form element', function() {
ok(view.$().find('form').get(0));
});
-test('submitting with invalid model does not call submit action on controller', function() {
- valid = false;
- view = Ember.View.create({
- template: templateFor('{{#formFor controller}}{{/formFor}}'),
- container: container,
- controller: controller
- });
- append(view);
+asyncTest('submitting with invalid model does not call submit action on controller', function() {
Ember.run(function() {
+ model.set('isValid', false);
+ view = Ember.View.create({
+ template: templateFor('{{#formFor controller}}{{/formFor}}'),
+ container: container,
+ controller: controller
+ });
+ append(view);
view._childViews[1].trigger('submit');
+ setTimeout(function() {
+ equal(controller.get('count'), 0);
+ start();
+ }, 50);
});
- equal(controller.get('count'), 0);
});
-test('submitting with valid model calls submit action on controller', function() {
- valid = true;
+asyncTest('submitting with valid model calls submit action on controller', function() {
+ model.set('isValid', true);
view = Ember.View.create({
template: templateFor('{{#formFor controller}}{{/formFor}}'),
container: container,
@@ -85,19 +90,26 @@ test('submitting with valid model calls submit action on controller', function()
Ember.run(function() {
view._childViews[1].trigger('submit');
});
- equal(controller.get('count'), 1);
+ setTimeout(function() {
+ equal(controller.get('count'), 1);
+ start();
+ }, 50);
});
-test('submitting with model that does not have validate method', function() {
- valid = true;
- view = Ember.View.create({
- template: templateFor('{{#formFor controller}}{{/formFor}}'),
- container: container,
- controller: controller
- });
- append(view);
+asyncTest('submitting with model that does not have validate method', function() {
+ delete model.validate;
Ember.run(function() {
+ model.set('isValid', true);
+ view = Ember.View.create({
+ template: templateFor('{{#formFor controller}}{{/formFor}}'),
+ container: container,
+ controller: controller
+ });
+ append(view);
view._childViews[1].trigger('submit');
+ setTimeout(function() {
+ equal(controller.get('count'), 1);
+ start();
+ }, 50);
});
- equal(controller.get('count'), 1);
});

0 comments on commit c51999c

Please sign in to comment.