File

src/app/components/cache-node.component.ts

Description

Loads data for a node from the cache or from the API.

Implements

OnInit OnDestroy

Metadata

selector [cache-node]
template
<ng-container *ngIf="node$.hasData"><ng-container *ngTemplateOutlet="itemTemplate; context: { $implicit: node$.current }"></ng-container></ng-container>

Index

Properties
Inputs

Constructor

constructor(dataService: DataService)
Parameters :
Name Type Optional
dataService DataService No

Inputs

lazy
Type : boolean
Default value : true

If true, this component will subscribe to the node lazily (i.e. it will not fetch new data if data is cached already)

node
Type : NodeId

The node that will be loaded.

Properties

itemTemplate
Decorators :
@ContentChild(ItemDirective, {read: TemplateRef})
node$
Type : DataNode<>
nodeSub
Type : Subscription
import {Component, ContentChild, Input, OnDestroy, OnInit, TemplateRef} from '@angular/core';
import {ItemDirective} from '@app/components/item.directive';
import DataService from '@app/data-dgql';
import {NodeId} from '@app/data-dgql/id';
import {DataNode} from '@app/data-dgql/query';
import {Subscription} from 'rxjs';

/**
 * Loads data for a node from the cache or from the API.
 */
@Component({
  // eslint-disable-next-line @angular-eslint/component-selector
  selector: '[cache-node]',
  template:
    '<ng-container *ngIf="node$.hasData"><ng-container *ngTemplateOutlet="itemTemplate; context: { $implicit: node$.current }"></ng-container></ng-container>'
})
export class CacheNodeComponent implements OnInit, OnDestroy {
  /** The node that will be loaded. */
  @Input() node: NodeId;
  /** If true, this component will subscribe to the node lazily (i.e. it will not fetch new data if data is cached already) */
  @Input() lazy = true;
  @ContentChild(ItemDirective, {read: TemplateRef}) itemTemplate;

  node$: DataNode<unknown>;
  nodeSub: Subscription;

  constructor(private dataService: DataService) {}

  ngOnInit() {
    this.node$ = this.dataService.getNode(this.node);
    this.nodeSub = this.lazy ? this.node$.subscribeLazy() : this.node$.subscribe();
  }
  ngOnDestroy() {
    this.nodeSub.unsubscribe();
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""