Skip to content

Commit

Permalink
feat(Form): model is passed as parameter to default and before actions
Browse files Browse the repository at this point in the history
Closes #126

BREAKING CHANGE: the signature of the default action that is called when submitting the form has changed.
    The form's model is always the first parameter, with an optional validation result as the second parameter

    Before:

    submit(validationResult) { ... }

    After:

    submit(model, validationResult) { ... }
  • Loading branch information
simonihmig committed Sep 13, 2016
1 parent 8274a55 commit 6c9282a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
7 changes: 4 additions & 3 deletions addon/components/bs-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,16 @@ export default Ember.Component.extend(ComponentParent, {
if (e) {
e.preventDefault();
}
let model = this.get('model');

this.sendAction('before');
this.sendAction('before', model);

if (!this.get('hasValidator')) {
return this.sendAction();
return this.sendAction('action', model);
} else {
let validationPromise = this.validate(this.get('model'));
if (validationPromise && validationPromise instanceof Ember.RSVP.Promise) {
validationPromise.then((r) => this.sendAction('action', r), (err) => {
validationPromise.then((r) => this.sendAction('action', model, r), (err) => {
this.get('childFormElements').setEach('showValidation', true);
return this.sendAction('invalid', err);
});
Expand Down
21 changes: 14 additions & 7 deletions tests/integration/components/bs-form-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,32 @@ test('form has correct CSS class', function(assert) {
});

test('Submitting the form calls default action', function(assert) {
this.on('testAction', () => {
let model = {};
this.set('model', model)
this.on('testAction', (m) => {
assert.ok(true, 'Default action has been called.');
assert.equal(m, model, 'Action invocation has model as parameter');
});
this.render(hbs`{{#bs-form action=(action "testAction")}}Test{{/bs-form}}`);
this.render(hbs`{{#bs-form model=model action=(action "testAction")}}Test{{/bs-form}}`);

assert.expect(1);
assert.expect(2);
this.$('form').submit();
});

test('Submitting the form calls before submit action', function(assert) {
this.on('beforeAction', () => {
let model = {};
this.set('model', model)
this.on('beforeAction', (m) => {
assert.ok(true, 'Before action has been called.');
assert.equal(m, model, 'Action invocation has model as parameter');
});
this.on('submitAction', () => {
this.on('submitAction', (m) => {
assert.ok(true, 'Default action has been called.');
assert.equal(m, model, 'Action invocation has model as parameter');
});
this.render(hbs`{{#bs-form before=(action "beforeAction") action=(action "submitAction")}}Test{{/bs-form}}`);
this.render(hbs`{{#bs-form model=model before=(action "beforeAction") action=(action "submitAction")}}Test{{/bs-form}}`);

assert.expect(2);
assert.expect(4);
this.$('form').submit();
});

Expand Down

0 comments on commit 6c9282a

Please sign in to comment.