File

packages/data/src/lib/entry/entry.directive.ts

Description

Loads an entry by id to the template. https://components.entrecode.de/entries/entries?e=1

Implements

OnChanges WithLoader

Metadata

Selector [ecEntry]

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor(sdk: SdkService, symbol: SymbolService, notificationService: NotificationsService)

Injects the sdk

Parameters :
Name Type Optional
sdk SdkService No
symbol SymbolService No
notificationService NotificationsService No

Inputs

autoload
Type : boolean

Should the entry be loaded immediately? Defaults to true

entryId
Type : string

The entry id that should be loaded

levels
Type : number

The levels to use.

loader
Type : LoaderComponent

The loader that should be used.

model
Type : string

The model to load from

Outputs

loaded
Type : EventEmitter<EntryResource>

Fires as soon as the entry has been loaded.

Methods

load
load()

Loads the entry. Can be called from template when using autoload=false

Returns : any
ngOnChanges
ngOnChanges()

as soon as model and id are known, the entry will be loaded.

Returns : void

Properties

entry
Type : any

The current loaded entry

Public notificationService
Type : NotificationsService
promise
Type : any

The loading promise

Public symbol
Type : SymbolService

ec-entry

This directive can be used to load a single entry directly from the template:

<h1 ecEntry model="muffin" entryId="SkXEhDZ5yW" #muffin="ecEntry"></h1>
{{muffin.entry?.name}}

import { Directive, EventEmitter, Input, OnChanges, Output } from '@angular/core';
import { SdkService } from '../sdk/sdk.service';
import EntryResource from 'ec.sdk/lib/resources/publicAPI/EntryResource';
import { WithLoader, LoaderComponent, NotificationsService } from '@ec.components/ui';
import { SymbolService } from '@ec.components/ui';

/** Loads an entry by id to the template.
 * <example-url>https://components.entrecode.de/entries/entries?e=1</example-url>
 * */
@Directive({
  selector: '[ecEntry]',
  exportAs: 'ecEntry',
})
export class EntryDirective implements OnChanges, WithLoader {
  /** The loading promise */
  promise: any;
  /** The entry id that should be loaded*/
  @Input() entryId: string;
  /** The model to load from */
  @Input() model: string;
  /** Should the entry be loaded immediately? Defaults to true */
  @Input() autoload: boolean;
  /** The levels to use. */
  @Input() levels: number;
  /** Fires as soon as the entry has been loaded. */
  @Output() loaded: EventEmitter<EntryResource> = new EventEmitter();
  /** The loader that should be used. */
  @Input() loader: LoaderComponent;
  /** The current loaded entry */
  entry: any;

  /** Injects the sdk */
  constructor(
    private sdk: SdkService,
    public symbol: SymbolService,
    public notificationService: NotificationsService,
  ) {}

  /** as soon as model and id are known, the entry will be loaded. */
  ngOnChanges() {
    if (this.autoload === false) {
      return;
    }
    this.load();
  }

  /** Loads the entry. Can be called from template when using autoload=false */
  load() {
    if (!this.entryId || !this.model) {
      return;
    }
    this.promise = this.sdk.api
      .entry(this.model, this.entryId, this.levels)
      .then((entry) => {
        this.entry = entry;
        this.loaded.emit(entry);
        return entry;
      })
      .catch((error) =>
        this.notificationService.emit({
          title: this.symbol.resolve('entry.load.error'),
          error,
        }),
      );
    if (this.loader) {
      this.loader.wait(this.promise);
    }
    return this.promise;
  }
}

result-matching ""

    No results matching ""