import {EventEmitter,OnInit, OnDestroy,Input,Output,Component} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {Subscription} from 'rxjs/Subscription'; import {Profile} from '../../models/profile.model'; import {ProfileService } from '../../services/profiles.service' @Component({ selector:'profile-edit', templateUrl:'app/profiles/detail/detail.component.html' }) export class ProfileEditController implements OnInit, OnDestroy { profile:Profile; username:string; private new:Boolean = false; usernameParamSubscription: Subscription; profileSubscription: Subscription; profileServiceSubscription: Subscription; constructor(private router: Router,private route:ActivatedRoute,private profileService:ProfileService){ this.profile = {}; } ngOnInit(){ this.usernameParamSubscription = this.route.params.map(p => p['username']).subscribe(username=>{ this.username = username; this.profileSubscription = this.profileService.getProfile(this.username).subscribe(profile=>{ this.profile = profile; }); }); } ngOnDestroy(){ if(this.usernameParamSubscription) this.usernameParamSubscription.unsubscribe(); if(this.profileSubscription) this.profileSubscription.unsubscribe(); if(this.profileServiceSubscription) this.profileServiceSubscription.unsubscribe(); } save($event:any, detailForm:any){ if(detailForm.valid){ this.profileServiceSubscription = this.profileService.editProfile(this.username,this.profile).subscribe(()=>{ this.router.navigate(['']); }); } return false; } } @Component({ selector:'profile-create', templateUrl:'app/profiles/detail/detail.component.html' }) export class ProfileCreateController implements OnInit, OnDestroy{ @Input() profile:Profile private new:Boolean=true; @Output() initialse:EventEmitter; @Output() finished:EventEmitter; profileServiceSubscription: Subscription; constructor(private router:Router, private profileService:ProfileService){ } ngOnInit(){ this.profile = {}; } ngOnDestroy(){ if(this.profileServiceSubscription) this.profileServiceSubscription.unsubscribe(); } save($event:any, detailForm:any){ if(detailForm.valid){ this.profileServiceSubscription = this.profileService.createProfile(this.profile).subscribe(()=>{ this.router.navigate(['']); }); } return false; } }