import { Component, Input, Output, OnInit, AfterViewInit, OnDestroy, ViewChild, EventEmitter } from '@angular/core';
import { ParentService } from '../services/parent.service';
import { Observable } from 'rxjs/Rx';
import { ChildComponent } from '../child.component';
@Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss']
})
export class ParentComponent implements OnInit, AfterViewInit, OnDestroy {
intervalId;
parentService;
dataSubscription;
syncData = {
val1: 0,
val2: 100
}
@Input() model = {};
@Input() index;
// parentCollapse is the name of the template variable #parentCollapse on parent element
@ViewChild('parentCollapse') parentCollapseChild;
@ViewChild(ChildComponent) childComponent;
ngAfterViewInit () {
$(this.parentCollapseChild.nativeElement).on( 'open.collapse.amui', this.openCollapsibleParentHandler.bind(this));
$(this.parentCollapseChild.nativeElement).on( 'close.collapse.amui', this.closeCollapsibleParentHandler.bind(this));
}
constructor(parentService: ParentService) {
this.parentService = parentService;
}
openCollapsibleParentHandler (event) {
if ( Object.is(this.parentCollapseChild.nativeElement, event.target) ) {
this.dataSubscription = this.showRealTimeData();
}
return true;
};
closeCollapsibleParentHandler (event) {
if ( Object.is(this.parentCollapseChild.nativeElement, event.target)) {
this.dataSubscription.unsubscribe();
}
return true;
};
ngOnInit() {}
showRealTimeData(pollingInterval: number = 1500) {
return this.parentService.connectData(pollingInterval).subscribe(
data => {
this.syncData.val1 = data.val1;
this.syncData.val2 = data.val2;
}
);
};
ngOnDestroy() {
if(this.dataSubscription) {
this.dataSubscription.unsubscribe();
}
}
}