<!DOCTYPE html>
<html>
<head>
<base href="." />
<title>angular2 playground</title>
<link rel="stylesheet" href="style.css" />
<script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js/dist/zone.js"></script>
<script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
<script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
<link rel="stylesheet" href="https://unpkg.com/purecss@0.6.1/build/pure-min.css" integrity="sha384-CCTZv2q9I9m3UOxRLaJneXrrqKwUNOzZ6NGEUMwHtShDJ+nCoiXJCAgi05KfkLGY" crossorigin="anonymous">
</head>
<body>
<my-app>
loading...
</my-app>
</body>
</html>
/* Styles go here */
.error-message {
color:red;
font-size: .75em;
margin-left: 10rem;
}
/* it has to be block for Pure (flex by default) */
dorf-field-wrapper {
display: block !important;
}
### DORF - angular, typescript, annotations
A simple example which points out how easily it can be to create Angular's Reactive Form with DORF library.
https://github.com/mat3e/dorf
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
//map tells the System loader where to look for things
map: {
'app': './src',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
'@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
'dorf': 'npm:dorf@3.1.0',
'rxjs': 'npm:rxjs',
'typescript': 'npm:typescript@2.0.2/lib/typescript.js'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
dorf: {
main: "./index.js",
defaultExtension: "js"
},
rxjs: {
defaultExtension: 'js'
}
}
});
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';
platformBrowserDynamic().bootstrapModule(AppModule)
//our root app component
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {DorfModule} from 'dorf'
import {Person} from './person/model'
import {PersonDetailComponent} from './person/person-detail.component'
@Component({
selector: "my-app",
template: `
<h2>Person in form: {{activeIndex >= 0 ? activePerson.fullname : "new person"}}</h2>
<person-details [domainObject]="activePerson" (createUpdate)=onCreateUpdate($event)></person-details>
<hr/>
<table class="pure-table pure-table-striped">
<tbody>
<tr *ngFor="let p of people; let i = index;">
<td>{{p.fullname}}</td><td><button (click)=onClick(i)>edit</button></td>
</tr>
</tbody>
</table>
`
})
export class App {
activeIndex = -1;
activePerson = new Person();
people: Person[] = [];
onClick(index: number) {
this.activeIndex = index;
this.activePerson = this.people[index];
}
onCreateUpdate(person: IPerson) {
this.activeIndex >= 0 ? this.people[this.activeIndex] = new Person(person)
: this.people.push(new Person(person));
this.reset();
}
private reset() {
this.activeIndex = -1;
this.activePerson = new Person();
}
}
@NgModule({
imports: [
BrowserModule,
DorfModule.forRoot({
css: {
form: "pure-form pure-form-aligned",
wrapper: "pure-control-group",
error: "error-message",
buttons: {
save: 'pure-button pure-button-primary',
reset: 'hidden',
group: 'pure-controls'
}
},
requiredWithStar: true
})
],
declarations: [ App, PersonDetailComponent ],
bootstrap: [ App ]
})
export class AppModule {}
import { Validators } from "@angular/forms";
import { DorfObject, DorfInput, DorfRadio, DorfSelect, DorfCheckbox } from "dorf";
/**
* Reactive Form will return set of properties, not a class with methods.
* It's recommended to store those properties in the interface and use them in class.
*/
export interface IPerson {
name: string;
surname: string;
gender: string;
age: number;
cardCode: number;
favColor: string;
smart: string;
}
/**
* Domain Object.
* Big part of the class is a standard definition - properties, getters, constructor (based on the interface).
* Real additional job is when providing FieldDefinitions.
*/
@DorfObject()
export class Person implements IPerson {
/*
Example properties.
*/
@DorfInput<string>({
label: "Name",
type: "text",
validator: Validators.required,
errorMessage: "Name is required",
updateModelOnChange: true
})
name: string;
@DorfInput<string>({
label: "Surname",
type: "text",
validator: Validators.required,
errorMessage: "Surname is required"
})
surname: string;
@DorfRadio<string>({
label: "Gender",
optionsToSelect: [
{ key: "m", value: "male" },
{ key: "f", value: "female" }
],
validator: Validators.required,
errorMessage: "Gender is required"
})
gender: string;
@DorfInput<number>({
label: "Age",
type: "number"
})
age: number;
@DorfInput<number>({
label: "Credit card PIN",
type: "password",
validator: Validators.pattern("[0-9]{4}"),
errorMessage: "PIN should contain just 4 digits"
})
cardCode: number;
@DorfSelect({
label: "Favourite color",
optionsToSelect: [
{ key: "#fff", value: "white" },
{ key: "#000", value: "black" },
{ key: "#ff0000", value: "red" },
{ key: "#00ff00", value: "green" },
{ key: "#0000ff", value: "blue" }
]
})
favColor: string;
@DorfCheckbox<string>({
innerLabel: "Is smart?",
mapping: { trueValue: "yes", falseValue: "no" }
})
smart: string;
/**
* Shortcut for getting name and surname pair.
*/
get fullname() {
return this.name + " " + this.surname;
}
/**
* Creation from the interface should be supported.
*/
constructor(base: IPerson = null) {
if (base) {
this.name = base.name;
this.surname = base.surname;
this.gender = base.gender;
this.age = base.age;
this.cardCode = base.cardCode;
this.favColor = base.favColor;
this.smart = base.smart;
}
}
}
import { Component, Output, EventEmitter } from "@angular/core";
import { DorfConfigService, IDorfForm, DorfObjectInput, DorfForm } from "dorf";
import { IPerson, Person } from "./model";
/**
* Lightweight class which creates a form.
* Template provided from the library, but it is possible to provide a custom one (which uses Dorf components).
*
* It's important to pass config in the constructor and define onSubmit method.
*/
@DorfForm()
@Component({
selector: "person-details"
})
export class PersonDetailComponent implements IDorfForm {
@DorfObjectInput() domainObject: Person;
@Output() createUpdate = new EventEmitter<IPerson>();
constructor(public config: DorfConfigService) { }
onSubmit() {
let result = this["form"].value as IPerson;
console.log(result);
this.createUpdate.emit(result);
}
}