Skip to content

Commit

Permalink
fix(menu): detech changes in items component
Browse files Browse the repository at this point in the history
So they are rerendered when context is updated
  • Loading branch information
Alex Malkevich committed Mar 28, 2019
1 parent c0819ee commit 9fb523d
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
Component,
OnInit,
ViewContainerRef,
ChangeDetectorRef,
DoCheck,
} from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter, map, startWith } from 'rxjs/operators';
Expand All @@ -22,8 +24,8 @@ export interface NgView<T, C = T> {
styleUrls: ['./dynamic-menu-items.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DynamicMenuItemsComponent implements OnInit {
ctx!: DynamicMenuTemplateContext;
export class DynamicMenuItemsComponent implements DoCheck {
ctx: DynamicMenuTemplateContext | undefined;

navigationEnd$ = this.router.events.pipe(
filter(e => e instanceof NavigationEnd),
Expand All @@ -36,18 +38,19 @@ export class DynamicMenuItemsComponent implements OnInit {

constructor(
private vcr: ViewContainerRef,
private cdr: ChangeDetectorRef,
private dynamicMenuService: DynamicMenuService,
private router: Router,
) {}

ngOnInit(): void {
const ctx = this.getTplContext((this.vcr as any)._view);
ngDoCheck(): void {
this.ctx = this.getTplContext((this.vcr as any)._view);

if (!ctx) {
if (!this.ctx) {
throw Error(`DynamicMenuItemsComponent: Used outside of context!`);
}

this.ctx = ctx;
this.cdr.markForCheck();
}

private getTplContext(view: NgView<any> | undefined) {
Expand All @@ -61,6 +64,10 @@ export class DynamicMenuItemsComponent implements OnInit {
}

private shouldRender() {
if (!this.ctx) {
return false;
}

const { parentConfig } = this.ctx;

if (parentConfig) {
Expand Down

0 comments on commit 9fb523d

Please sign in to comment.