Betaimport { Component, Element } from '@stencil/core';
import {
ShowObjectsEvent,
ShowObjectsEventName,
} from '@limetech/lime-web-components';
@Component({ tag: 'my-deal-list' })
export class MyDealList {
@Element()
private host: HTMLElement;
public connectedCallback() {
this.host.addEventListener(ShowObjectsEventName, this.onShowObjects);
}
public disconnectedCallback() {
this.host.removeEventListener(ShowObjectsEventName, this.onShowObjects);
}
private onShowObjects = (event: Event) => {
const { limetype, filter } = (event as ShowObjectsEvent).detail;
if (limetype !== 'deal') {
// This list shows no other objects, so the sender keeps its default
return;
}
// Show the objects here, so the sender skips its default
event.preventDefault();
console.log('Show the deals that match', filter);
};
}
import {
Operator,
ShowObjectsEvent,
ShowObjectsEventDetail,
ShowObjectsEventName,
} from '@limetech/lime-web-components';
const link = document.querySelector<HTMLElement>('my-link');
const onClick = (click: MouseEvent) => {
const detail: ShowObjectsEventDetail = {
limetype: 'deal',
filter: {
op: Operator.AND,
exp: [
{ key: 'company', op: Operator.EQUALS, exp: 1001 },
{
type: 'filter',
key: '_id',
op: Operator.IN,
exp: 'webclient.deal.ongoing',
},
],
},
};
const showObjects: ShowObjectsEvent = new CustomEvent(
ShowObjectsEventName,
{
bubbles: true,
composed: true,
cancelable: true,
detail: detail,
}
);
link?.dispatchEvent(showObjects);
if (showObjects.defaultPrevented) {
// A listener shows the objects, so skip the link
click.preventDefault();
}
};
link?.addEventListener('click', onClick);
The event a component dispatches when the user asks to see some objects
The sender dispatches it on its own element, with
bubbles,composedandcancelableset totrue. The event reaches only the components the sender sits inside, so a listener adds itself to its own element and hears only the senders inside it. The detail says which objects to show, so a listener can act on it without knowing which component dispatched it.A listener that shows the objects itself calls
preventDefault()before it returns, because the sender checks the event as soon as the dispatch returns. The sender then skips its own default, for example opening the objects in the explorer. When nobody cancels the event, the sender does what it does without a listener.The event must be cancelable. Without
cancelable,preventDefault()does nothing, so the sender also runs its default and the user gets both.