AbstractWidget
Base class for widgets for WidgetRegistrySingleton.
Example:
export default class OpenMenuWidget extends AbstractWidget {
constructor(element) {
super(element);
this._onClickBound = (...args) => {
this._onClick(...args);
};
this.element.addEventListener('click', this._onClickBound);
}
_onClick = (e) => {
e.preventDefault();
console.log('I should have opened the menu here');
}
destroy() {
this.element.removeEventListener('click', this._onClickBound);
}
}
<button type="button" data-ievv-jsbase-widget="open-menu-button">
Open menu
</button>
export default class OpenMenuWidget extends AbstractWidget {
constructor(element) {
super(element);
this._onClickBound = (...args) => {
this._onClick(...args);
};
this.element.addEventListener('click', this._onClickBound);
}
getDefaultConfig() {
return {
'menuId': 'id_main_menu';
}
}
_onClick = (e) => {
e.preventDefault();
console.log(`I should have opened the menu with id="${this.config.menuId}" here`);
}
destroy() {
this.element.removeEventListener('click', this._onClickBound);
}
}
<!-- Using the default config -->
<button type="button" data-ievv-jsbase-widget="open-menu-button">
Open the main menu
</button>
<!-- Override the menuId config -->
<button type="button" data-ievv-jsbase-widget="open-menu-button"
data-ievv-jsbase-widget-config='{"menuId": "id_the_other_menu"}'>
Open the other menu
</button>
Constructor Summary
Public Constructor | ||
public |
constructor(element: Element) |
Method Summary
Public Methods | ||
public |
destroy() Destroy the widget. |
|
public |
Get the default config. |
Public Constructors
Public Members
public get config: Object: * source
Get the config.
JSON decodes any config supplied via the data-ievv-jsbase-widget-config
attribute of the Element and AbstractWidget#getDefaultConfig
into a config object. The result of this is cached, so multiple calls
to this property will only result in the config object being created
once.
Return:
Object | The config object. This will be an empty object
if we have no AbstractWidget#getDefaultConfig and
no config is supplied via the |
Throw:
If the |
public element: * source
Public Methods
public destroy() source
Destroy the widget.
You should override this in subclasses if your widget sets up something that will work incorrectly if the widget disappears or is re-created (such as event listeners and signals).