Skip to content

Commit d6cda15

Browse files
committed
refactor(form): misc minor refactoring
Closes angular#3951
1 parent 73351ac commit d6cda15

File tree

16 files changed

+160
-197
lines changed

16 files changed

+160
-197
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class CheckboxControlValueAccessor implements ControlValueAccessor {
4040
cd.valueAccessor = this;
4141
}
4242

43-
writeValue(value: any) { setProperty(this._renderer, this._elementRef, "checked", value); }
43+
writeValue(value: any): void { setProperty(this._renderer, this._elementRef, "checked", value); }
4444

4545
get ngClassUntouched(): boolean {
4646
return isPresent(this._cd.control) ? this._cd.control.untouched : false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Form} from './form_interface';
22
import {AbstractControlDirective} from './abstract_control_directive';
33

44
/**
5-
* A directive that contains a group of [NgControl].
5+
* A directive that contains multiple {@link NgControl}.
66
*
77
* Only used by the forms module.
88
*/

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ export class DefaultValueAccessor implements ControlValueAccessor {
4141
cd.valueAccessor = this;
4242
}
4343

44-
writeValue(value: any) {
45-
// both this.value and setProperty are required at the moment
46-
// remove when a proper imperative API is provided
44+
writeValue(value: any): void {
4745
var normalizedValue = isBlank(value) ? '' : value;
4846
setProperty(this._renderer, this._elementRef, 'value', normalizedValue);
4947
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const controlGroupBinding =
4242
* `})
4343
* class SignupComp {
4444
* onSignUp(value) {
45-
* // value === {personal: {name: 'some name'},
45+
* // value === {
46+
* // personal: {name: 'some name'},
4647
* // credentials: {login: 'some login', password: 'some password'}}
4748
* }
4849
* }
@@ -63,9 +64,9 @@ export class NgControlGroup extends ControlContainer implements OnInit,
6364
this._parent = _parent;
6465
}
6566

66-
onInit() { this.formDirective.addControlGroup(this); }
67+
onInit(): void { this.formDirective.addControlGroup(this); }
6768

68-
onDestroy() { this.formDirective.removeControlGroup(this); }
69+
onDestroy(): void { this.formDirective.removeControlGroup(this); }
6970

7071
get control(): ControlGroup { return this.formDirective.getControlGroup(this); }
7172

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,23 @@ const controlNameBinding =
2424
*
2525
* In this example, we create the login and password controls.
2626
* We can work with each control separately: check its validity, get its value, listen to its
27-
changes.
27+
* changes.
2828
*
2929
* ```
3030
* @Component({selector: "login-comp"})
3131
* @View({
3232
* directives: [FORM_DIRECTIVES],
3333
* template: `
34-
* <form #f="form" (submit)='onLogIn(f.value)'>
35-
* Login <input type='text' ng-control='login' #l="form">
36-
* <div *ng-if="!l.valid">Login is invalid</div>
34+
* <form #f="form" (submit)='onLogIn(f.value)'>
35+
* Login <input type='text' ng-control='login' #l="form">
36+
* <div *ng-if="!l.valid">Login is invalid</div>
3737
*
38-
* Password <input type='password' ng-control='password'>
39-
40-
* <button type='submit'>Log in!</button>
41-
* </form>
38+
* Password <input type='password' ng-control='password'>
39+
* <button type='submit'>Log in!</button>
40+
* </form>
4241
* `})
4342
* class LoginComp {
44-
* onLogIn(value) {
43+
* onLogIn(value): void {
4544
* // value === {login: 'some login', password: 'some password'}
4645
* }
4746
* }
@@ -54,17 +53,17 @@ const controlNameBinding =
5453
* @View({
5554
* directives: [FORM_DIRECTIVES],
5655
* template: `
57-
* <form (submit)='onLogIn()'>
58-
* Login <input type='text' ng-control='login' [(ng-model)]="credentials.login">
59-
* Password <input type='password' ng-control='password'
60-
[(ng-model)]="credentials.password">
61-
* <button type='submit'>Log in!</button>
62-
* </form>
56+
* <form (submit)='onLogIn()'>
57+
* Login <input type='text' ng-control='login' [(ng-model)]="credentials.login">
58+
* Password <input type='password' ng-control='password'
59+
* [(ng-model)]="credentials.password">
60+
* <button type='submit'>Log in!</button>
61+
* </form>
6362
* `})
6463
* class LoginComp {
6564
* credentials: {login:string, password:string};
6665
*
67-
* onLogIn() {
66+
* onLogIn(): void {
6867
* // this.credentials.login === "some login"
6968
* // this.credentials.password === "some password"
7069
* }
@@ -94,18 +93,18 @@ export class NgControlName extends NgControl implements OnChanges,
9493
this.validators = validators;
9594
}
9695

97-
onChanges(c: StringMap<string, any>) {
96+
onChanges(changes: StringMap<string, any>) {
9897
if (!this._added) {
9998
this.formDirective.addControl(this);
10099
this._added = true;
101100
}
102-
if (isPropertyUpdated(c, this.viewModel)) {
101+
if (isPropertyUpdated(changes, this.viewModel)) {
103102
this.viewModel = this.model;
104103
this.formDirective.updateModel(this, this.model);
105104
}
106105
}
107106

108-
onDestroy() { this.formDirective.removeControl(this); }
107+
onDestroy(): void { this.formDirective.removeControl(this); }
109108

110109
viewToModelUpdate(newValue: any): void {
111110
this.viewModel = newValue;

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ const formDirectiveBinding =
4242
* </form>
4343
* `})
4444
* class SignupComp {
45-
* onSignUp(value) {
46-
* // value === {personal: {name: 'some name'},
45+
* onSignUp(value): void {
46+
* // value === {
47+
* // personal: {name: 'some name'},
4748
* // credentials: {login: 'some login', password: 'some password'}}
4849
* }
4950
* }
@@ -60,14 +61,9 @@ const formDirectiveBinding =
6061
exportAs: 'form'
6162
})
6263
export class NgForm extends ControlContainer implements Form {
63-
form: ControlGroup;
64+
form: ControlGroup = new ControlGroup({});
6465
ngSubmit = new EventEmitter();
6566

66-
constructor() {
67-
super();
68-
this.form = new ControlGroup({});
69-
}
70-
7167
get formDirective(): Form { return this; }
7268

7369
get control(): ControlGroup { return this.form; }
@@ -79,10 +75,10 @@ export class NgForm extends ControlContainer implements Form {
7975
addControl(dir: NgControl): void {
8076
this._later(_ => {
8177
var container = this._findContainer(dir.path);
82-
var c = new Control();
83-
setUpControl(c, dir);
84-
container.addControl(dir.name, c);
85-
c.updateValidity();
78+
var ctrl = new Control();
79+
setUpControl(ctrl, dir);
80+
container.addControl(dir.name, ctrl);
81+
ctrl.updateValidity();
8682
});
8783
}
8884

@@ -101,9 +97,9 @@ export class NgForm extends ControlContainer implements Form {
10197
addControlGroup(dir: NgControlGroup): void {
10298
this._later(_ => {
10399
var container = this._findContainer(dir.path);
104-
var c = new ControlGroup({});
105-
container.addControl(dir.name, c);
106-
c.updateValidity();
100+
var group = new ControlGroup({});
101+
container.addControl(dir.name, group);
102+
group.updateValidity();
107103
});
108104
}
109105

@@ -123,8 +119,8 @@ export class NgForm extends ControlContainer implements Form {
123119

124120
updateModel(dir: NgControl, value: any): void {
125121
this._later(_ => {
126-
var c = <Control>this.form.find(dir.path);
127-
c.updateValue(value);
122+
var ctrl = <Control>this.form.find(dir.path);
123+
ctrl.updateValue(value);
128124
});
129125
}
130126

@@ -138,9 +134,5 @@ export class NgForm extends ControlContainer implements Form {
138134
return ListWrapper.isEmpty(path) ? this.form : <ControlGroup>this.form.find(path);
139135
}
140136

141-
_later(fn) {
142-
var c: PromiseCompleter<any> = PromiseWrapper.completer();
143-
PromiseWrapper.then(c.promise, fn, (_) => {});
144-
c.resolve(null);
145-
}
137+
_later(fn): void { PromiseWrapper.then(PromiseWrapper.resolve(null), fn, (_) => {}); }
146138
}

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ const formControlBinding =
1717
* # Example
1818
*
1919
* In this example, we bind the control to an input element. When the value of the input element
20-
* changes, the value of
21-
* the control will reflect that change. Likewise, if the value of the control changes, the input
22-
* element reflects that
23-
* change.
20+
* changes, the value of the control will reflect that change. Likewise, if the value of the
21+
* control changes, the input element reflects that change.
2422
*
2523
* ```
2624
* @Component({selector: "login-comp"})
@@ -29,16 +27,12 @@ const formControlBinding =
2927
* template: "<input type='text' [ng-form-control]='loginControl'>"
3028
* })
3129
* class LoginComp {
32-
* loginControl:Control;
33-
*
34-
* constructor() {
35-
* this.loginControl = new Control('');
36-
* }
30+
* loginControl: Control = new Control('');;
3731
* }
3832
*
3933
* ```
4034
*
41-
* We can also use ng-model to bind a domain model to the form.
35+
* We can also use `ng-model` to bind a domain model to the form.
4236
*
4337
* ```
4438
* @Component({selector: "login-comp"})
@@ -47,12 +41,8 @@ const formControlBinding =
4741
* template: "<input type='text' [ng-form-control]='loginControl' [(ng-model)]='login'>"
4842
* })
4943
* class LoginComp {
50-
* loginControl:Control;
44+
* loginControl: Control = new Control('');
5145
* login:string;
52-
*
53-
* constructor() {
54-
* this.loginControl = new Control('');
55-
* }
5646
* }
5747
* ```
5848
*/
@@ -76,13 +66,13 @@ export class NgFormControl extends NgControl implements OnChanges {
7666
this.validators = validators;
7767
}
7868

79-
onChanges(c: StringMap<string, any>) {
69+
onChanges(changes: StringMap<string, any>): void {
8070
if (!this._added) {
8171
setUpControl(this.form, this);
8272
this.form.updateValidity();
8373
this._added = true;
8474
}
85-
if (isPropertyUpdated(c, this.viewModel)) {
75+
if (isPropertyUpdated(changes, this.viewModel)) {
8676
this.form.updateValue(this.model);
8777
this.viewModel = this.model;
8878
}

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ const formDirectiveBinding =
2121
* # Example
2222
*
2323
* In this example, we bind the control group to the form element, and we bind the login and
24-
* password controls to the
25-
* login and password elements.
24+
* password controls to the login and password elements.
2625
*
2726
* ```
2827
* @Component({selector: "login-comp"})
2928
* @View({
3029
* directives: [FORM_DIRECTIVES],
31-
* template: "<form [ng-form-model]='loginForm'>" +
32-
* "Login <input type='text' ng-control='login'>" +
33-
* "Password <input type='password' ng-control='password'>" +
34-
* "<button (click)="onLogin()">Login</button>" +
35-
* "</form>"
30+
* template: `
31+
* <form [ng-form-model]='loginForm'>
32+
* Login <input type='text' ng-control='login'>
33+
* Password <input type='password' ng-control='password'>
34+
* <button (click)="onLogin()">Login</button>
35+
* </form>`
3636
* })
3737
* class LoginComp {
38-
* loginForm:ControlGroup;
38+
* loginForm: ControlGroup;
3939
*
4040
* constructor() {
4141
* this.loginForm = new ControlGroup({
@@ -44,7 +44,7 @@ const formDirectiveBinding =
4444
* });
4545
* }
4646
*
47-
* onLogin() {
47+
* onLogin(): void {
4848
* // this.loginForm.value
4949
* }
5050
* }
@@ -57,15 +57,17 @@ const formDirectiveBinding =
5757
* @Component({selector: "login-comp"})
5858
* @View({
5959
* directives: [FORM_DIRECTIVES],
60-
* template: "<form [ng-form-model]='loginForm'>" +
61-
* "Login <input type='text' ng-control='login' [(ng-model)]='login'>" +
62-
* "Password <input type='password' ng-control='password' [(ng-model)]='password'>" +
63-
* "<button (click)="onLogin()">Login</button>" +
64-
* "</form>"
60+
* template: `
61+
* <form [ng-form-model]='loginForm'>
62+
* Login <input type='text' ng-control='login' [(ng-model)]='credentials.login'>
63+
* Password <input type='password' ng-control='password'
64+
* [(ng-model)]='credentials.password'>
65+
* <button (click)="onLogin()">Login</button>
66+
* </form>`
6567
* })
6668
* class LoginComp {
67-
* credentials:{login:string, password:string}
68-
* loginForm:ControlGroup;
69+
* credentials: {login: string, password: string};
70+
* loginForm: ControlGroup;
6971
*
7072
* constructor() {
7173
* this.loginForm = new ControlGroup({
@@ -74,7 +76,7 @@ const formDirectiveBinding =
7476
* });
7577
* }
7678
*
77-
* onLogin() {
79+
* onLogin(): void {
7880
* // this.credentials.login === 'some login'
7981
* // this.credentials.password === 'some password'
8082
* }
@@ -85,9 +87,7 @@ const formDirectiveBinding =
8587
selector: '[ng-form-model]',
8688
bindings: [formDirectiveBinding],
8789
properties: ['form: ng-form-model'],
88-
host: {
89-
'(submit)': 'onSubmit()',
90-
},
90+
host: {'(submit)': 'onSubmit()'},
9191
events: ['ngSubmit'],
9292
exportAs: 'form'
9393
})
@@ -97,7 +97,7 @@ export class NgFormModel extends ControlContainer implements Form,
9797
directives: NgControl[] = [];
9898
ngSubmit = new EventEmitter();
9999

100-
onChanges(_) { this._updateDomValue(); }
100+
onChanges(_): void { this._updateDomValue(); }
101101

102102
get formDirective(): Form { return this; }
103103

@@ -106,9 +106,9 @@ export class NgFormModel extends ControlContainer implements Form,
106106
get path(): string[] { return []; }
107107

108108
addControl(dir: NgControl): void {
109-
var c: any = this.form.find(dir.path);
110-
setUpControl(c, dir);
111-
c.updateValidity();
109+
var ctrl: any = this.form.find(dir.path);
110+
setUpControl(ctrl, dir);
111+
ctrl.updateValidity();
112112
this.directives.push(dir);
113113
}
114114

@@ -125,8 +125,8 @@ export class NgFormModel extends ControlContainer implements Form,
125125
}
126126

127127
updateModel(dir: NgControl, value: any): void {
128-
var c  = <Control>this.form.find(dir.path);
129-
c.updateValue(value);
128+
var ctrl  = <Control>this.form.find(dir.path);
129+
ctrl.updateValue(value);
130130
}
131131

132132
onSubmit(): boolean {
@@ -136,8 +136,8 @@ export class NgFormModel extends ControlContainer implements Form,
136136

137137
_updateDomValue() {
138138
ListWrapper.forEach(this.directives, dir => {
139-
var c: any = this.form.find(dir.path);
140-
dir.valueAccessor.writeValue(c.value);
139+
var ctrl: any = this.form.find(dir.path);
140+
dir.valueAccessor.writeValue(ctrl.value);
141141
});
142142
}
143143
}

0 commit comments

Comments
 (0)