Skip to content

Commit 7c6130c

Browse files
committed
feat(core): desugar [()] to [prop] and (prop-change)
BREAKING CHANGE Before ``` <cmp [(prop)]="field"> was desugared to <cmp [prop]="field" (prop)="field=$event"> ``` After ``` <cmp [(prop)]="field"> is desugared to <cmp [prop]="field" (prop-change)="field=$event"> ``` Closes angular#4658
1 parent df09389 commit 7c6130c

File tree

9 files changed

+33
-21
lines changed

9 files changed

+33
-21
lines changed

modules/angular2/src/core/compiler/template_parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ class TemplateParseVisitor implements HtmlAstVisitor {
361361

362362
private _parseAssignmentEvent(name: string, expression: string, sourceInfo: string,
363363
targetMatchableAttrs: string[][], targetEvents: BoundEventAst[]) {
364-
this._parseEvent(name, `${expression}=$event`, sourceInfo, targetMatchableAttrs, targetEvents);
364+
this._parseEvent(`${name}-change`, `${expression}=$event`, sourceInfo, targetMatchableAttrs,
365+
targetEvents);
365366
}
366367

367368
private _parseEvent(name: string, expression: string, sourceInfo: string,

modules/angular2/src/core/forms/directives/ng_control_name.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const controlNameBinding =
7575
selector: '[ng-control]',
7676
bindings: [controlNameBinding],
7777
inputs: ['name: ngControl', 'model: ngModel'],
78-
outputs: ['update: ngModel'],
78+
outputs: ['update: ngModelChange'],
7979
exportAs: 'form'
8080
})
8181
export class NgControlName extends NgControl implements OnChanges,

modules/angular2/src/core/forms/directives/ng_form_control.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const formControlBinding =
6666
selector: '[ng-form-control]',
6767
bindings: [formControlBinding],
6868
inputs: ['form: ngFormControl', 'model: ngModel'],
69-
outputs: ['update: ngModel'],
69+
outputs: ['update: ngModelChange'],
7070
exportAs: 'form'
7171
})
7272
export class NgFormControl extends NgControl implements OnChanges {

modules/angular2/src/core/forms/directives/ng_model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const formControlBinding = CONST_EXPR(new Binding(NgControl, {toAlias: forwardRe
3737
selector: '[ng-model]:not([ng-control]):not([ng-form-control])',
3838
bindings: [formControlBinding],
3939
inputs: ['model: ngModel'],
40-
outputs: ['update: ngModel'],
40+
outputs: ['update: ngModelChange'],
4141
exportAs: 'form'
4242
})
4343
export class NgModel extends NgControl implements OnChanges {

modules/angular2/test/core/compiler/template_parser_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export function main() {
282282
],
283283
[
284284
BoundEventAst,
285-
'prop',
285+
'propChange',
286286
null,
287287
'v = $event',
288288
'TestComp > div:nth-child(0)[[(prop)]=v]'
@@ -305,7 +305,7 @@ export function main() {
305305
],
306306
[
307307
BoundEventAst,
308-
'prop',
308+
'propChange',
309309
null,
310310
'v = $event',
311311
'TestComp > div:nth-child(0)[bindon-prop=v]'

modules/angular2/test/core/linker/integration_spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,9 @@ export function main() {
839839
rootTC.debugElement.componentInstance.ctxProp = 'one';
840840
rootTC.detectChanges();
841841

842-
expect(dir.value).toEqual('one');
842+
expect(dir.control).toEqual('one');
843843

844-
ObservableWrapper.subscribe(dir.control, (_) => {
844+
ObservableWrapper.subscribe(dir.controlChange, (_) => {
845845
expect(rootTC.debugElement.componentInstance.ctxProp).toEqual('two');
846846
async.done();
847847
});
@@ -2072,15 +2072,13 @@ class ToolbarComponent {
20722072
}
20732073
}
20742074

2075-
@Directive({selector: '[two-way]', inputs: ['value: control'], outputs: ['control']})
2075+
@Directive({selector: '[two-way]', inputs: ['control'], outputs: ['controlChange']})
20762076
@Injectable()
20772077
class DirectiveWithTwoWayBinding {
2078-
control: EventEmitter;
2079-
value: any;
2078+
controlChange = new EventEmitter();
2079+
control = null;
20802080

2081-
constructor() { this.control = new EventEmitter(); }
2082-
2083-
triggerChange(value) { ObservableWrapper.callNext(this.control, value); }
2081+
triggerChange(value) { ObservableWrapper.callNext(this.controlChange, value); }
20842082
}
20852083

20862084
@Injectable()

modules/upgrade/src/ng1_facade.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ export class ExportedNg1Component {
6363
localName = localName.substr(1) || name;
6464
var outputName = 'output_' + name;
6565
var outputNameRename = outputName + ': ' + name;
66+
var outputNameRenameChange = outputName + ': ' + name + 'Change';
6667
var inputName = 'input_' + name;
6768
var inputNameRename = inputName + ': ' + name;
6869
switch (type) {
6970
case '=':
7071
this.propertyOutputs.push(outputName);
7172
this.checkProperties.push(localName);
7273
this.outputs.push(outputName);
73-
this.outputsRename.push(outputNameRename);
74+
this.outputsRename.push(outputNameRenameChange);
7475
this.propertyMap[outputName] = localName;
7576
// don't break; let it fall through to '@'
7677
case '@':

modules/upgrade/src/ng2_facade.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,23 @@ export class Ng2ComponentFacade {
120120
var output = outputs[j];
121121
var expr = null;
122122
var assignExpr = false;
123+
124+
var bindonAttr =
125+
output.bindonAttr ? output.bindonAttr.substring(0, output.bindonAttr.length - 6) : null;
126+
var bracketParenAttr =
127+
output.bracketParenAttr ?
128+
`[(${output.bracketParenAttr.substring(2, output.bracketParenAttr.length - 8)})]` :
129+
null;
130+
123131
if (attrs.hasOwnProperty(output.onAttr)) {
124132
expr = attrs[output.onAttr];
125133
} else if (attrs.hasOwnProperty(output.parenAttr)) {
126134
expr = attrs[output.parenAttr];
127-
} else if (attrs.hasOwnProperty(output.bindonAttr)) {
128-
expr = attrs[output.bindonAttr];
135+
} else if (attrs.hasOwnProperty(bindonAttr)) {
136+
expr = attrs[bindonAttr];
129137
assignExpr = true;
130-
} else if (attrs.hasOwnProperty(output.bracketParenAttr)) {
131-
expr = attrs[output.bracketParenAttr];
138+
} else if (attrs.hasOwnProperty(bracketParenAttr)) {
139+
expr = attrs[bracketParenAttr];
132140
assignExpr = true;
133141
}
134142

modules/upgrade/test/integration_spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ export function main() {
111111
Component({
112112
selector: 'ng2',
113113
inputs: ['literal', 'interpolate', 'oneWayA', 'oneWayB', 'twoWayA', 'twoWayB'],
114-
outputs:
115-
['eventA', 'eventB', 'twoWayAEmitter: twoWayA', 'twoWayBEmitter: twoWayB']
114+
outputs: [
115+
'eventA',
116+
'eventB',
117+
'twoWayAEmitter: twoWayAChange',
118+
'twoWayBEmitter: twoWayBChange'
119+
]
116120
})
117121
.View({
118122
template:

0 commit comments

Comments
 (0)