...
Info |
---|
Only |
mgsGetJqueryWidgetInstance
Helper function that returns jQuery widget instance.
Source: MageSuite_ThemeHelpers/js/utils/get-jquery-widget-instance.js
Requirejs alias: mgsGetJqueryWidgetInstance
Example:
Code Block | ||
---|---|---|
| ||
define('mgsGetJqueryWidgetInstance', function (getJqueryWidgetInstance) {
return async function () {
const swatchesWidgetHtmlElement = document.querySelector('[data-role=swatch-options]');
if (!swatchesWidgetHtmlElement) {
return
}
const swatchesWidget = await getJqueryWidgetInstance({
element: swatchesWidgetHtmlElement,
widgetName: 'mage-SwatchRenderer',
widgetCreateEventName: 'swatchrenderercreate'
}, true);
}
}) |
That block of code will bring you swatch renderer widget instance.
Params description and how to get them:
First argument: widget data (
object
type)element
: html element (not jQuery object) that widget is attatched to. You can check this in two ways:by widget initialization script:
"[data-role=swatch-options]"
,by
console.log(this.element)
in one of the widget's methods,
widgetName
: jQuery widget name. You can check it in two ways:open devtools, click on element to which widget is attached and select “Properties” tab:
console.log(this.widgetFullName)
in one of the widget's methods
widgetCreateEventName
- this parameter is required if you pass second (wait
) parameter astrue
. It's event name that's triggered after widget initialization. In 99% of cases it's`${widget's name}create`.toLowerCase()
. How to get widget name:it’s word after prefix in widgets initialization:
$.widget('mage.SwatchRenderer', {...
<-swatchrenderer
,you can
console.log(this.widgetName)
in one of the widget's methods,if above methods don’t work, you can always put
console.log(event)
somewhere here: lib/web/jquery/ui-modules/widget::_trigger. This will list you all widgets create events, so you can find yours.
Second argument:
wait
(boolean
type):false
(default) - if widget is not initialized yet, you will get null as promise resolve,true
- function will wait until widget is initialized.