4

Is there any option to init (text/x-magento-init) script after page load? Example, I am working on a simple quickview feature and my controller returns my a layout - phtml file - with rendered html and script with config like this below:

<script type="text/x-magento-init">
{
    "[data-role=swatch-options]": {
        "Magento_Swatches/js/swatch-renderer": {
            "jsonConfig": ...
        }
    }
}

New content is appended to modal wrapper and new popup is initialized. Is there any option to fire (maybe some kind of event od body/document) which triggers again init process for Magento_Swatches/js/swatch-renderer script?

1
  • Did you get to the bottom of how to do this. I need to initialise swatches on ajax loaded product list as not working with infinite scrolls so may be similar?
    – harri
    Sep 19, 2018 at 10:09

2 Answers 2

0

MAy be you try afterRender function to initialize function after load something you want, Take a look on following document : Js Referrence

1
  • 1
    thank for your reply! Nice try, but I am working with mixin which based on $.widget (mage.catalogAddToCart), so KO template functions are not visible there
    – Rob
    Jan 24, 2018 at 12:06
0

I believe you need to use .trigger('contentUpdated') When you get the Ajax Content trigger the contentUpdated event so the new data-mage-init to be evaluated. e.g.

var contentBlock = this.element.find('.page-footer');
    $.ajax({
        url: this.options.contentsUrl,
        type: 'GET',
        dataType: 'html',
        data: {
            'form_key': FORM_KEY,
            node: node.id
        },
        context: contentBlock,
        showLoader: true
    }).done(function (data) {
        contentBlock.html(data).trigger('contentUpdated');
    });

credits to this answare.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.