Skip to content

Commit ed83647

Browse files
committed
refactor(form example): TSify
1 parent 6c1cb08 commit ed83647

File tree

1 file changed

+57
-62
lines changed

1 file changed

+57
-62
lines changed

modules/examples/src/forms/index.es6 renamed to modules/examples/src/forms/index.ts

Lines changed: 57 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import {bootstrap, NgIf, NgFor, EventEmitter} from 'angular2/angular2';
2-
import {FormBuilder, Validators, formDirectives, ControlGroup} from 'angular2/forms';
3-
4-
// TODO(radokirov): Once the application is transpiled by TS instead of Traceur,
5-
// add those imports back into 'angular2/angular2';
6-
import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations';
7-
import {View} from 'angular2/src/core/annotations_impl/view';
1+
import {bootstrap, NgIf, NgFor, EventEmitter, Component, View} from 'angular2/angular2';
2+
import {
3+
FormBuilder,
4+
Validators,
5+
formDirectives,
6+
ControlGroup,
7+
Control,
8+
ControlArray
9+
} from 'angular2/forms';
10+
11+
import {ObservableWrapper} from 'angular2/src/facade/async';
12+
import {print} from 'angular2/src/facade/lang';
813

914
// HeaderFields renders the bound header control group. It can used as follows:
1015
//
1116
// <survey-header [header]="header"></survey-header>
1217
//
1318
// This component is self-contained and can be tested in isolation.
14-
@Component({
15-
selector: 'survey-header',
16-
properties: {
17-
"header" : "header"
18-
}
19-
})
19+
@Component({selector: 'survey-header', properties: {"header": "header"}})
2020
@View({
2121
template: `
2222
<div [control-group]="header">
@@ -45,7 +45,7 @@ import {View} from 'angular2/src/core/annotations_impl/view';
4545
directives: [formDirectives, NgIf]
4646
})
4747
class HeaderFields {
48-
header:ControlGroup;
48+
header: ControlGroup;
4949
}
5050

5151

@@ -59,10 +59,7 @@ class HeaderFields {
5959
@Component({
6060
selector: 'survey-question',
6161
events: ['destroy'],
62-
properties: {
63-
"question" : "question",
64-
"index" : "index"
65-
}
62+
properties: {"question": "question", "index": "index"}
6663
})
6764
@View({
6865
template: `
@@ -104,28 +101,23 @@ class HeaderFields {
104101
directives: [formDirectives, NgIf]
105102
})
106103
class SurveyQuestion {
107-
question:ControlGroup;
108-
index:number;
109-
destroy:EventEmitter;
104+
question: ControlGroup;
105+
index: number;
106+
destroy: EventEmitter;
110107

111-
constructor() {
112-
this.destroy = new EventEmitter();
113-
}
108+
constructor() { this.destroy = new EventEmitter(); }
114109

115-
deleteQuestion() {
110+
deleteQuestion(): void {
116111
// Invoking an injected event emitter will fire an event,
117112
// which in this case will result in calling `deleteQuestion(i)`
118-
this.destroy.next(null);
113+
ObservableWrapper.callNext(this.destroy, null);
119114
}
120115
}
121116

122117

123118

124119
// SurveyBuilder is a form that allows you to create a survey.
125-
@Component({
126-
selector: 'survey-builder-app',
127-
appInjector: [FormBuilder]
128-
})
120+
@Component({selector: 'survey-builder-app', appInjector: [FormBuilder]})
129121
@View({
130122
template: `
131123
<h1>Create New Survey</h1>
@@ -147,54 +139,57 @@ class SurveyQuestion {
147139
directives: [formDirectives, NgFor, HeaderFields, SurveyQuestion]
148140
})
149141
class SurveyBuilder {
150-
form:ControlGroup;
151-
builder:FormBuilder;
152-
153-
constructor(b:FormBuilder) {
154-
this.builder = b;
155-
this.form = b.group({
156-
"header" : b.group({
157-
"title" : ["", Validators.required],
158-
"description" : ["", Validators.required],
159-
"date" : ""
142+
form: ControlGroup;
143+
144+
constructor(public builder: FormBuilder) {
145+
this.form = builder.group({
146+
"header": builder.group({
147+
"title": ["", Validators.required],
148+
"description": ["", Validators.required],
149+
"date": ""
160150
}),
161-
"questions": b.array([])
151+
"questions": builder.array([])
162152
});
163153
}
164154

165-
addQuestion() {
166-
var newQuestion = this.builder.group({
167-
"type": ["", Validators.required],
168-
"questionText": ["", Validators.required],
169-
"responseLength": [100, Validators.required]
170-
}, {
171-
// Optional controls can be dynamically added or removed from the form.
172-
// Here, the responseLength field is optional and not included by default.
173-
"optionals": {
174-
"responseLength": false
175-
}
176-
});
155+
addQuestion(): void {
156+
var newQuestion: ControlGroup = this.builder.group(
157+
{
158+
"type": ["", Validators.required],
159+
"questionText": ["", Validators.required],
160+
"responseLength": [100, Validators.required]
161+
},
162+
{
163+
// Optional controls can be dynamically added or removed from the form.
164+
// Here, the responseLength field is optional and not included by default.
165+
"optionals": {"responseLength": false}
166+
});
177167

178168
// Every Control has an observable of value changes. You can subscribe to this observable
179169
// to update the form, update the application model, etc.
180170
// These observables can also be transformed and combined. This enables implementing
181171
// complex form interactions in a declarative fashion.
182172
//
183173
// We are disabling the responseLength control when the question type is checkbox.
184-
newQuestion.controls.type.valueChanges.observer({
185-
next: (v) => v == 'text' || v == 'textarea' ? newQuestion.include('responseLength') : newQuestion.exclude('responseLength')
186-
});
174+
var typeCtrl: Control = newQuestion.controls['type'];
175+
176+
ObservableWrapper.subscribe(typeCtrl.valueChanges,
177+
(v) => v == 'text' || v == 'textarea' ?
178+
newQuestion.include('responseLength') :
179+
newQuestion.exclude('responseLength'));
187180

188-
this.form.controls.questions.push(newQuestion);
181+
(<ControlArray>this.form.controls['questions']).push(newQuestion);
189182
}
190183

191-
destroyQuestion(index:number) {
192-
this.form.controls.questions.removeAt(index);
184+
destroyQuestion(index: number): void {
185+
(<ControlArray>this.form.controls['questions']).removeAt(index);
193186
}
194187

195-
submitForm() {
196-
console.log("Submitting a form")
197-
console.log("value", this.form.value, "valid", this.form.valid, "errors", this.form.errors);
188+
submitForm(): void {
189+
print('Submitting a form');
190+
print(`"value: ${this.form.value}`);
191+
print(` valid: $ { this.form.valid } `);
192+
print(` errors: $ { this.form.errors }`);
198193
}
199194
}
200195

0 commit comments

Comments
 (0)