AbstractWidget
Base class for widgets for WidgetRegistrySingleton.
Example:
export default class OpenMenuWidget extends AbstractWidget {
constructor(element, widgetInstanceId) {
super(element, widgetInstanceId);
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, widgetInstanceId: string) |
Member Summary
Public Members | ||
public get |
Get the config. |
|
public |
element: * |
|
public |
|
Method Summary
Public Methods | ||
public |
Called after all the widgets within the element that WidgetRegistrySingleton#initializeAllWidgetsWithinElement was called with is initialized. |
|
public |
destroy() Destroy the widget. |
|
public |
Get the default config. |
|
public |
If you override AbstractWidget#afterInitializeAllWidgets,
you must override this to return |
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 widgetInstanceId: * source
Public Methods
public afterInitializeAllWidgets() source
Called after all the widgets within the element that WidgetRegistrySingleton#initializeAllWidgetsWithinElement was called with is initialized.
For performance reasons, this is only called if
AbstractWidget#useAfterInitializeAllWidgets returns
true
, so you must also override that method if you override
this method.
This is useful if you need to do something after other widgets have finished initializing, which may be the case for loosely coupled widgets.
Does nothing by default.
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).
public getDefaultConfig(): Object source
Get the default config.
Any config supplied via the data-ievv-jsbase-widget-config
attribute is merged into this object.
public useAfterInitializeAllWidgets(): boolean source
If you override AbstractWidget#afterInitializeAllWidgets,
you must override this to return true
.
Return:
boolean | Should return |