Skip to content

Commit a73c643

Browse files
committed
feat(forms): remove support for declaring forms in html
1 parent ded83e5 commit a73c643

File tree

3 files changed

+53
-156
lines changed

3 files changed

+53
-156
lines changed

modules/angular2/forms.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './src/forms/model';
22
export * from './src/forms/directives';
3-
export * from './src/forms/validators';
4-
export * from './src/forms/validator_directives';
3+
export * from './src/forms/validators';

modules/angular2/src/forms/directives.js

Lines changed: 20 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection';
55
import {ControlGroup, Control} from './model';
66
import * as validators from './validators';
77

8-
class ControlGroupDirectiveBase {
9-
addDirective(directive):void {}
10-
findControl(name:string):Control { return null; }
11-
}
12-
138
@CONST()
149
export class ControlValueAccessor {
1510
readValue(el){}
@@ -60,9 +55,16 @@ function controlValueAccessorFor(controlType:string):ControlValueAccessor {
6055
}
6156
}
6257

63-
64-
export class ControlDirectiveBase {
65-
_groupDecorator:ControlGroupDirectiveBase;
58+
@Decorator({
59+
lifecycle: [onChange],
60+
selector: '[control]',
61+
bind: {
62+
'controlName' : 'control',
63+
'type' : 'type'
64+
}
65+
})
66+
export class ControlDirective {
67+
_groupDecorator:ControlGroupDirective;
6668
_el:NgElement;
6769

6870
controlName:string;
@@ -71,12 +73,18 @@ export class ControlDirectiveBase {
7173

7274
validator:Function;
7375

74-
constructor(groupDecorator, el:NgElement) {
76+
constructor(@Ancestor() groupDecorator:ControlGroupDirective, el:NgElement) {
7577
this._groupDecorator = groupDecorator;
7678
this._el = el;
7779
this.validator = validators.nullValidator;
7880
}
7981

82+
// TODO: vsavkin this should be moved into the constructor once static bindings
83+
// are implemented
84+
onChange(_) {
85+
this._initialize();
86+
}
87+
8088
_initialize() {
8189
this._groupDecorator.addDirective(this);
8290

@@ -105,51 +113,15 @@ export class ControlDirectiveBase {
105113
}
106114
}
107115

108-
@Decorator({
109-
lifecycle: [onChange],
110-
selector: '[control-name]',
111-
bind: {
112-
'controlName' : 'control-name',
113-
'type' : 'type'
114-
}
115-
})
116-
export class ControlNameDirective extends ControlDirectiveBase {
117-
constructor(@Ancestor() groupDecorator:ControlGroupDirective, el:NgElement) {
118-
super(groupDecorator, el);
119-
}
120-
121-
onChange(_) {
122-
this._initialize();
123-
}
124-
}
125-
126-
@Decorator({
127-
lifecycle: [onChange],
128-
selector: '[control]',
129-
bind: {
130-
'controlName' : 'control',
131-
'type' : 'type'
132-
}
133-
})
134-
export class ControlDirective extends ControlDirectiveBase {
135-
constructor(@Ancestor() groupDecorator:NewControlGroupDirective, el:NgElement) {
136-
super(groupDecorator, el);
137-
}
138-
139-
onChange(_) {
140-
this._initialize();
141-
}
142-
}
143-
144116
@Decorator({
145117
selector: '[control-group]',
146118
bind: {
147119
'controlGroup' : 'control-group'
148120
}
149121
})
150-
export class ControlGroupDirective extends ControlGroupDirectiveBase {
122+
export class ControlGroupDirective {
151123
_controlGroup:ControlGroup;
152-
_directives:List<ControlNameDirective>;
124+
_directives:List<ControlDirective>;
153125

154126
constructor() {
155127
super();
@@ -161,71 +133,15 @@ export class ControlGroupDirective extends ControlGroupDirectiveBase {
161133
ListWrapper.forEach(this._directives, (cd) => cd._updateDomValue());
162134
}
163135

164-
addDirective(c:ControlNameDirective) {
165-
ListWrapper.push(this._directives, c);
166-
}
167-
168-
findControl(name:string):Control {
169-
return this._controlGroup.controls[name];
170-
}
171-
}
172-
173-
@Component({
174-
selector: '[new-control-group]',
175-
bind: {
176-
'initData' : 'new-control-group'
177-
}
178-
})
179-
@Template({inline: '<content>'})
180-
export class NewControlGroupDirective extends ControlGroupDirectiveBase {
181-
_initData:any;
182-
_controlGroup:ControlGroup;
183-
_directives:List<ControlNameDirective>;
184-
185-
constructor() {
186-
super();
187-
this._directives = ListWrapper.create();
188-
}
189-
190-
set initData(value) {
191-
this._initData = value;
192-
}
193-
194136
addDirective(c:ControlDirective) {
195137
ListWrapper.push(this._directives, c);
196-
this._controlGroup = null;
197138
}
198139

199140
findControl(name:string):Control {
200-
if (isBlank(this._controlGroup)) {
201-
this._controlGroup = this._createControlGroup();
202-
}
203141
return this._controlGroup.controls[name];
204142
}
205-
206-
_createControlGroup():ControlGroup {
207-
var controls = ListWrapper.reduce(this._directives, (memo, cd) => {
208-
var initControlValue = this._initData[cd.controlName];
209-
memo[cd.controlName] = new Control(initControlValue);
210-
return memo;
211-
}, {});
212-
return new ControlGroup(controls);
213-
}
214-
215-
get value() {
216-
return this._controlGroup.value;
217-
}
218-
219-
get errors() {
220-
return this._controlGroup.errors;
221-
}
222-
223-
get valid() {
224-
return this._controlGroup.valid;
225-
}
226143
}
227144

228145
export var FormDirectives = [
229-
ControlGroupDirective, ControlNameDirective,
230-
ControlDirective, NewControlGroupDirective
146+
ControlGroupDirective, ControlDirective
231147
];

modules/angular2/test/forms/integration_spec.js

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
1515
import {Injector} from 'angular2/di';
1616

1717
import {Component, Decorator, Template} from 'angular2/core';
18-
import {ControlGroupDirective, ControlNameDirective,
19-
ControlDirective, NewControlGroupDirective,
20-
Control, ControlGroup, ControlValueAccessor,
21-
RequiredValidatorDirective} from 'angular2/forms';
18+
import {ControlGroupDirective, ControlDirective, Control, ControlGroup,
19+
ControlValueAccessor, RequiredValidatorDirective} from 'angular2/forms';
20+
21+
import * as validators from 'angular2/src/forms/validators';
2222

2323
export function main() {
2424
function detectChanges(view) {
@@ -42,8 +42,7 @@ export function main() {
4242

4343
tplResolver.setTemplate(componentType, new Template({
4444
inline: template,
45-
directives: [ControlGroupDirective, ControlNameDirective, ControlDirective,
46-
NewControlGroupDirective, WrappedValue]
45+
directives: [ControlGroupDirective, ControlDirective, WrappedValue]
4746
}));
4847

4948
compiler.compile(componentType).then((pv) => {
@@ -61,7 +60,7 @@ export function main() {
6160
}));
6261

6362
var t = `<div [control-group]="form">
64-
<input type="text" control-name="login">
63+
<input type="text" control="login">
6564
</div>`;
6665

6766
compile(MyComp, t, ctx, (view) => {
@@ -78,7 +77,7 @@ export function main() {
7877
var ctx = new MyComp(form);
7978

8079
var t = `<div [control-group]="form">
81-
<input type="text" control-name="login">
80+
<input type="text" control="login">
8281
</div>`;
8382

8483
compile(MyComp, t, ctx, (view) => {
@@ -99,7 +98,7 @@ export function main() {
9998
var ctx = new MyComp(form);
10099

101100
var t = `<div [control-group]="form">
102-
<input type="text" control-name="login">
101+
<input type="text" control="login">
103102
</div>`;
104103

105104
compile(MyComp, t, ctx, (view) => {
@@ -121,7 +120,7 @@ export function main() {
121120
}), "one");
122121

123122
var t = `<div [control-group]="form">
124-
<input type="text" [control-name]="name">
123+
<input type="text" [control]="name">
125124
</div>`;
126125

127126
compile(MyComp, t, ctx, (view) => {
@@ -141,7 +140,7 @@ export function main() {
141140
var ctx = new MyComp(new ControlGroup({"checkbox": new Control(true)}));
142141

143142
var t = `<div [control-group]="form">
144-
<input type="checkbox" control-name="checkbox">
143+
<input type="checkbox" control="checkbox">
145144
</div>`;
146145

147146
compile(MyComp, t, ctx, (view) => {
@@ -160,7 +159,7 @@ export function main() {
160159
var ctx = new MyComp(new ControlGroup({"name": new Control("aa")}));
161160

162161
var t = `<div [control-group]="form">
163-
<input type="text" control-name="name" wrapped-value>
162+
<input type="text" control="name" wrapped-value>
164163
</div>`;
165164

166165
compile(MyComp, t, ctx, (view) => {
@@ -176,48 +175,37 @@ export function main() {
176175
});
177176
});
178177

179-
describe("declarative forms", () => {
180-
it("should initialize dom elements", (done) => {
181-
var t = `<div [new-control-group]="{'login': 'loginValue', 'password':'passValue'}">
182-
<input type="text" id="login" control="login">
183-
<input type="password" id="password" control="password">
184-
</div>`;
185-
186-
compile(MyComp, t, new MyComp(), (view) => {
187-
var loginInput = queryView(view, "#login")
188-
expect(loginInput.value).toEqual("loginValue");
178+
describe("validations", () => {
179+
it("should use validators defined in html",(done) => {
180+
var form = new ControlGroup({"login": new Control("aa")});
181+
var ctx = new MyComp(form);
189182

190-
var passInput = queryView(view, "#password")
191-
expect(passInput.value).toEqual("passValue");
192-
193-
done();
194-
});
195-
});
183+
var t = `<div [control-group]="form">
184+
<input type="text" control="login" required>
185+
</div>`;
196186

197-
it("should update the control group values on DOM change", (done) => {
198-
var t = `<div #form [new-control-group]="{'login': 'loginValue'}">
199-
<input type="text" control="login">
200-
</div>`;
187+
compile(MyComp, t, ctx, (view) => {
188+
expect(form.valid).toEqual(true);
201189

202-
compile(MyComp, t, new MyComp(), (view) => {
203-
var input = queryView(view, "input")
190+
var input = queryView(view, "input");
204191

205-
input.value = "updatedValue";
192+
input.value = "";
206193
dispatchEvent(input, "change");
207194

208-
var form = view.contextWithLocals.get("form");
209-
expect(form.value).toEqual({'login': 'updatedValue'});
195+
expect(form.valid).toEqual(false);
210196
done();
211197
});
212198
});
213199

214-
it("should support validators",(done) => {
215-
var t = `<div #form [new-control-group]="{'login': 'loginValue'}">
216-
<input type="text" control="login" required>
217-
</div>`;
200+
it("should use validators defined in the model",(done) => {
201+
var form = new ControlGroup({"login": new Control("aa", validators.required)});
202+
var ctx = new MyComp(form);
203+
204+
var t = `<div [control-group]="form">
205+
<input type="text" control="login">
206+
</div>`;
218207

219-
compile(MyComp, t, new MyComp(), (view) => {
220-
var form = view.contextWithLocals.get("form");
208+
compile(MyComp, t, ctx, (view) => {
221209
expect(form.valid).toEqual(true);
222210

223211
var input = queryView(view, "input");
@@ -236,12 +224,6 @@ export function main() {
236224
@Component({
237225
selector: "my-comp"
238226
})
239-
@Template({
240-
inline: "",
241-
directives: [ControlGroupDirective, ControlNameDirective,
242-
ControlDirective, NewControlGroupDirective, RequiredValidatorDirective,
243-
WrappedValue]
244-
})
245227
class MyComp {
246228
form:ControlGroup;
247229
name:string;
@@ -266,7 +248,7 @@ class WrappedValueAccessor extends ControlValueAccessor {
266248
selector:'[wrapped-value]'
267249
})
268250
class WrappedValue {
269-
constructor(cd:ControlNameDirective) {
251+
constructor(cd:ControlDirective) {
270252
cd.valueAccessor = new WrappedValueAccessor();
271253
}
272254
}

0 commit comments

Comments
 (0)