In an Angular application, I'm creating a VexFlow renderer:
ngAfterViewInit() {
this.createSheet();
}
private createSheet() {
if (this.soundtrack != null) {
if (this.soundtrack.hasNotes()) {
this.sheetService.createSoundtrackSheet(this.name, this.soundtrack);
}
}
}
using the service method:
private VF = vexflow.Flow;
private renderContext(name: string, width: number, height: number) {
const elementName = ELEMENT_PREFIX + name;
const element = document.getElementById(elementName);
const renderer = new this.VF.Renderer(element, this.VF.Renderer.Backends.SVG);
renderer.resize(width, height);
return renderer.getContext();
}
I'm passing a DOM reference document.getElementById(elementName);
to the renderer constructor.
Should I do some freeing in the ngOnDestroy()
component method ?
UPDATE: This is how the actual full component looks like:
export class SheetComponent implements OnInit, AfterViewInit {
@Input() soundtrack: Soundtrack;
@Input() device: Device;
name: string;
constructor(
private sheetService: SheetService
) { }
ngOnInit() {
this.initializeName();
}
ngAfterViewInit() {
this.createSheet();
}
private initializeName() {
if (this.soundtrack != null) {
this.name = NAME_PREFIX_SOUNDTRACK + this.soundtrack.name;
} else if (this.device != null) {
this.name = NAME_PREFIX_DEVICE + this.device.name;
}
}
private createSheet() {
if (this.soundtrack != null) {
if (this.soundtrack.hasNotes()) {
this.sheetService.createSoundtrackSheet(this.name, this.soundtrack);
}
} else if (this.device != null) {
this.sheetService.createDeviceSheet(this.name, this.device);
}
}
}
with the template:
<div id="{{name}}"></div>
Now, maybe there's a way to use a @ViewChild(name) name: ElementRef;
annotation, but this doesn't compile:
export class SheetComponent implements OnInit, AfterViewInit {
@Input() soundtrack: Soundtrack;
@Input() device: Device;
name: string;
@ViewChild(name) sheetElement: ElementRef;
constructor(
private sheetService: SheetService
) { }
ngOnInit() {
this.initializeName();
}
ngAfterViewInit() {
this.createSheet();
}
private initializeName() {
if (this.soundtrack != null) {
this.name = NAME_PREFIX_SOUNDTRACK + this.soundtrack.name;
} else if (this.device != null) {
this.name = NAME_PREFIX_DEVICE + this.device.name;
}
}
private createSheet() {
if (this.soundtrack != null) {
if (this.soundtrack.hasNotes()) {
this.sheetService.createSoundtrackSheet(this.sheetElement.nativeElement.id, this.soundtrack);
// this.soundtrackStore.setSoundtrackSheet(this.name, sheet); TODO
}
} else if (this.device != null) {
this.sheetService.createDeviceSheet(this.sheetElement.nativeElement.id, this.device);
// this.deviceStore.setDeviceSheet(this.name, sheet); TODO
}
}
}