Magento2 JavaScript – Run JavaScript After data-mage-init / mage.apply() Has Finished Loading

javascriptmagento2uicomponent

There is a widget loaded by data-mage-init that I need to run code after it has triggered the change event on the product options, why it does this I don't know, I commented out the line that triggers the change event but the module still works perfectly.

I need to alter some other things on the page when the drop downs are changed. The problem is the drop downs are changed on load because data-mage-init loads this widget which uses trigger() to call the change event on all the select drop downs. I need to define my change events after this runs, otherwise I will have code executing just on page load, instead of when a drop down is changed, which is bad.

I am looking for a simple hook to execute code after ALL mage-init scripts have been looped over and loaded.

Best Answer

I think this is not the perfect answer this is a hint.

@Wolfe -can you find a data-bind attribute, if can I can give you a hint. Knockout Js use render events eg. afterRender, beforeRender like

Then if you can add afterRender event and can call your custom function. eg.

    <span class="counter-number" data-bind="text: getQuantity(),afterRender:setSummeryQty"></span>

afterRender:setSummeryQty here call the setSummeryQty() js function,