File

projects/storefrontlib/cms-components/product/carousel/product-carousel/product-carousel.component.ts

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector cx-product-carousel
templateUrl ./product-carousel.component.html

Index

Properties

Constructor

constructor(componentData: CmsComponentData<model>, productService: ProductService)
Parameters :
Name Type Optional
componentData CmsComponentData<model> No
productService ProductService No

Properties

Private componentData$
Type : Observable<model>
Default value : this.componentData.data$.pipe( filter(Boolean) )
items$
Type : Observable<Observable[]>
Default value : this.componentData$.pipe( map((data) => data.productCodes?.trim().split(' ') ?? []), map((codes) => codes.map((code) => this.productService.get(code, [this.PRODUCT_SCOPE, ProductScope.PRICE]) ) ) )

Observable that holds an Array of Observables. This is done, so that the component UI could consider to lazy load the UI components when they're in the viewpoint.

Protected Readonly PRODUCT_SCOPE
Default value : ProductScope.LIST
title$
Type : Observable<string>
Default value : this.componentData$.pipe( map((data) => data.title) )

returns an Observable string for the title.

import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CmsProductCarouselComponent as model,
  Product,
  ProductScope,
  ProductService,
} from '@spartacus/core';
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { CmsComponentData } from '../../../../cms-structure/page/model/cms-component-data';

@Component({
  selector: 'cx-product-carousel',
  templateUrl: './product-carousel.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductCarouselComponent {
  protected readonly PRODUCT_SCOPE = ProductScope.LIST;

  private componentData$: Observable<model> = this.componentData.data$.pipe(
    filter(Boolean)
  );

  /**
   * returns an Observable string for the title.
   */
  title$: Observable<string> = this.componentData$.pipe(
    map((data) => data.title)
  );

  /**
   * Observable that holds an Array of Observables. This is done, so that
   * the component UI could consider to lazy load the UI components when they're
   * in the viewpoint.
   */
  items$: Observable<Observable<Product>[]> = this.componentData$.pipe(
    map((data) => data.productCodes?.trim().split(' ') ?? []),
    map((codes) =>
      codes.map((code) =>
        this.productService.get(code, [this.PRODUCT_SCOPE, ProductScope.PRICE])
      )
    )
  );

  constructor(
    protected componentData: CmsComponentData<model>,
    protected productService: ProductService
  ) {}
}
<cx-carousel
  [items]="items$ | async"
  [title]="title$ | async"
  [template]="carouselItem"
  itemWidth="285px"
>
</cx-carousel>

<ng-template #carouselItem let-item="item">
  <a tabindex="0" [routerLink]="{ cxRoute: 'product', params: item } | cxUrl">
    <cx-media
      [container]="item.images?.PRIMARY"
      format="product"
      [alt]="item.name"
    ></cx-media>
    <h3>
      {{ item.name }}
    </h3>
    <div class="price">
      {{ item.price?.formattedValue }}
    </div>
  </a>
</ng-template>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""