2016-09-06 12:43:07 +00:00
|
|
|
import { Component, OnInit } from "@angular/core";
|
|
|
|
import { Router } from "@angular/router";
|
|
|
|
|
|
|
|
import { Duck } from "./models";
|
|
|
|
import { DuckService } from "./duck.service";
|
|
|
|
|
|
|
|
@Component({
|
2016-10-27 15:52:55 +00:00
|
|
|
moduleId: module.id,
|
2016-09-06 12:43:07 +00:00
|
|
|
selector: "ducks",
|
2016-10-27 15:52:55 +00:00
|
|
|
templateUrl: "ducks.component.html"
|
2016-09-06 12:43:07 +00:00
|
|
|
})
|
2016-10-27 15:52:55 +00:00
|
|
|
export class DucksComponent {
|
|
|
|
ducks: Duck[];
|
2016-09-06 12:43:07 +00:00
|
|
|
selectedDuck: Duck;
|
|
|
|
|
2016-10-27 15:52:55 +00:00
|
|
|
constructor(private duckService: DuckService,
|
|
|
|
private router: Router)
|
2016-09-06 12:43:07 +00:00
|
|
|
{}
|
|
|
|
|
2016-10-27 15:52:55 +00:00
|
|
|
getDucks(): void
|
|
|
|
{
|
|
|
|
this.duckService
|
|
|
|
.getDucks()
|
|
|
|
.then(ducks => this.ducks = ducks);
|
2016-09-06 12:43:07 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 15:52:55 +00:00
|
|
|
add(name: string): void {
|
|
|
|
name = name.trim();
|
2016-09-06 12:43:07 +00:00
|
|
|
|
2016-10-27 15:52:55 +00:00
|
|
|
if (!name) { return; }
|
2016-09-06 14:49:18 +00:00
|
|
|
|
2016-10-27 15:52:55 +00:00
|
|
|
this.duckService.create(name)
|
2016-09-06 14:49:18 +00:00
|
|
|
.then(duck => {
|
|
|
|
this.ducks.push(duck);
|
|
|
|
this.selectedDuck = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-27 15:52:55 +00:00
|
|
|
ngOnInit(): void {
|
|
|
|
this.getDucks();
|
|
|
|
}
|
2016-09-06 14:49:18 +00:00
|
|
|
|
2016-10-27 15:52:55 +00:00
|
|
|
onSelect(duck: Duck): void {
|
|
|
|
this.selectedDuck = duck;
|
2016-09-06 14:49:18 +00:00
|
|
|
}
|
2016-09-06 12:43:07 +00:00
|
|
|
}
|