UNPKG

@gdwc/components

Version:

A library of generic web components that are accessible, framework agnostic, possible to style, and easy to use with data provided by Drupal

39 lines (33 loc) 879 B
import { DrupalState } from '@gdwc/drupal-state'; export class StoreController { constructor( host, apiBase, apiPrefix = 'jsonapi', defaultLocale = '', debug = false ) { // Store a reference to the host this.host = host; this.store = new DrupalState({ apiBase, apiPrefix, defaultLocale, debug, }); this.storeEvent = new CustomEvent('storeupdate', { bubbles: true, // bubble event to containing elements composed: true, // let the event pass through the shadowDOM boundary }); // Register for lifecycle updates host.addController(this); } hostUpdate() { this.host.dispatchEvent(this.storeEvent); } async query({ objectName, params, id }) { const result = await this.store.getObject({ objectName, id, params }); this.host.requestUpdate(); return result; } }