Skip to content

Commit 7fa6f51

Browse files
authored
Merge pull request #94 from yapplabs/use-closure-actions-internally
Use closure actions internally
2 parents fab8a18 + f09ff8a commit 7fa6f51

File tree

4 files changed

+69
-41
lines changed

4 files changed

+69
-41
lines changed

addon/components/radio-button-input.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,27 @@ export default Component.extend({
4040
return isEqual(this.get('groupValue'), this.get('value'));
4141
}).readOnly(),
4242

43-
sendChangedAction() {
43+
invokeChangedAction() {
4444
let value = this.get('value');
45+
let changedAction = this.get('changed');
4546

46-
if (typeof this.get('changed') === 'function') {
47-
return this.get('changed')(value);
47+
if (typeof changedAction === 'string') {
48+
this.sendAction('changed', value);
49+
return;
4850
}
4951

50-
this.sendAction('changed', value);
52+
if (changedAction) {
53+
changedAction(value);
54+
}
5155
},
5256

5357
change() {
5458
let value = this.get('value');
5559
let groupValue = this.get('groupValue');
5660

5761
if (groupValue !== value) {
58-
this.set('groupValue', value); // violates DDAU
59-
run.once(this, 'sendChangedAction');
62+
this.set('groupValue', value);
63+
run.once(this, 'invokeChangedAction');
6064
}
6165
}
6266
});

addon/components/radio-button.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@ export default Component.extend({
3838

3939
actions: {
4040
changed(newValue) {
41-
if (typeof this.get('changed') === 'function') {
42-
return this.get('changed')(newValue);
41+
let changedAction = this.get('changed');
42+
43+
// support legacy actions
44+
if (typeof changedAction === 'string') {
45+
this.sendAction('changed', newValue);
46+
return;
4347
}
4448

45-
this.sendAction('changed', newValue);
49+
// providing a closure action is optional
50+
if (changedAction) {
51+
changedAction(newValue);
52+
}
4653
}
4754
}
4855
});

addon/templates/components/radio-button.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
value=value
1313
ariaLabelledby=ariaLabelledby
1414
ariaDescribedby=ariaDescribedby
15-
changed="changed"}}
15+
changed=(action "changed")}}
1616

1717
{{yield}}
1818
</label>
@@ -29,5 +29,5 @@
2929
value=value
3030
ariaLabelledby=ariaLabelledby
3131
ariaDescribedby=ariaDescribedby
32-
changed="changed"}}
32+
changed=(action "changed")}}
3333
{{/if}}

tests/unit/components/radio-button-test.js

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
} from 'ember-qunit';
77
import hbs from 'htmlbars-inline-precompile';
88
import { alice, alice2, bob } from '../../helpers/person';
9-
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
109

1110
moduleForComponent('radio-button', 'RadioButtonComponent', {
1211
integration: true
@@ -39,7 +38,7 @@ test('it updates when clicked, and triggers the `changed` action', function(asse
3938
{{radio-button
4039
groupValue=groupValue
4140
value='component-value'
42-
changed='changed'
41+
changed=(action "changed")
4342
}}
4443
`);
4544

@@ -56,6 +55,28 @@ test('it updates when clicked, and triggers the `changed` action', function(asse
5655
assert.equal(changedActionCallCount, 1);
5756
});
5857

58+
test('when no action is passed, updating does not error', function(assert) {
59+
assert.expect(3);
60+
61+
this.set('groupValue', 'initial-group-value');
62+
63+
this.render(hbs`
64+
{{radio-button
65+
groupValue=groupValue
66+
value='component-value'
67+
}}
68+
`);
69+
70+
assert.equal(this.$('input').prop('checked'), false, 'starts unchecked');
71+
72+
run(() => {
73+
this.$('input').trigger('click');
74+
});
75+
76+
assert.equal(this.$('input').prop('checked'), true, 'updates element property');
77+
assert.equal(this.get('groupValue'), 'component-value', 'updates groupValue');
78+
});
79+
5980
test('it updates when the browser change event is fired', function(assert) {
6081
let changedActionCallCount = 0;
6182
this.on('changed', () => {
@@ -68,7 +89,7 @@ test('it updates when the browser change event is fired', function(assert) {
6889
{{radio-button
6990
groupValue=groupValue
7091
value='component-value'
71-
changed='changed'
92+
changed=(action "changed")
7293
}}
7394
`);
7495

@@ -94,7 +115,6 @@ test('it gives the label of a wrapped checkbox a `checked` className', function(
94115
{{#radio-button
95116
groupValue=groupValue
96117
value=value
97-
changed='changed'
98118
classNames='blue-radio'
99119
~}}
100120
Blue
@@ -121,7 +141,6 @@ test('providing `checkedClass` gives the label a custom classname when the radio
121141
groupValue=groupValue
122142
value=value
123143
checkedClass="my-custom-class"
124-
changed='changed'
125144
classNames='blue-radio'
126145
~}}
127146
Blue
@@ -364,35 +383,33 @@ test('it binds `aria-describedby` when specified', function(assert) {
364383
assert.equal(this.$('input').attr('aria-describedby'), 'green-label');
365384
});
366385

367-
if (hasEmberVersion(1, 13)) {
368-
test('it updates when clicked, and triggers the `changed` action when a closure action is passed in', function(assert) {
369-
assert.expect(5);
386+
test('it updates when clicked, and supports legacy-style string `changed` actions', function(assert) {
387+
assert.expect(5);
370388

371-
let changedActionCallCount = 0;
372-
this.on('changed', function() {
373-
changedActionCallCount++;
374-
});
389+
let changedActionCallCount = 0;
390+
this.on('changed', function() {
391+
changedActionCallCount++;
392+
});
375393

376-
this.set('groupValue', 'initial-group-value');
394+
this.set('groupValue', 'initial-group-value');
377395

378-
this.render(hbs`
379-
{{radio-button
380-
groupValue=groupValue
381-
value='component-value'
382-
changed=(action 'changed')
383-
}}
384-
`);
396+
this.render(hbs`
397+
{{radio-button
398+
groupValue=groupValue
399+
value="component-value"
400+
changed="changed"
401+
}}
402+
`);
385403

386-
assert.equal(changedActionCallCount, 0);
387-
assert.equal(this.$('input').prop('checked'), false);
404+
assert.equal(changedActionCallCount, 0);
405+
assert.equal(this.$('input').prop('checked'), false);
388406

389-
run(() => {
390-
this.$('input').trigger('click');
391-
});
407+
run(() => {
408+
this.$('input').trigger('click');
409+
});
392410

393-
assert.equal(this.$('input').prop('checked'), true, 'updates element property');
394-
assert.equal(this.get('groupValue'), 'component-value', 'updates groupValue');
411+
assert.equal(this.$('input').prop('checked'), true, 'updates element property');
412+
assert.equal(this.get('groupValue'), 'component-value', 'updates groupValue');
395413

396-
assert.equal(changedActionCallCount, 1);
397-
});
398-
}
414+
assert.equal(changedActionCallCount, 1);
415+
});

0 commit comments

Comments
 (0)