4

I am in need to trigger some script with "text/x-magento-init" on ajax call but I am unable to do so.

trigger('contentUpdated') 

contentUpdated is also not working in my case.

I am using contentUpdated as:

$('.page-footer').html(response.categoryProducts);                        
$('.page-footer').trigger('contentUpdated');

On console log(response.categoryProducts) I can see that x-magento-init script but the effect is not shown on frontend on using this way.

Any help ??

3

I found a solution for my question at last. I used 'applyBindings()' as 'contentUpdated' was not working in my case. Like

$('.page-footer').replaceWith(data.categoryProducts);
$('.page-footer').trigger('contentUpdated');
if ($.fn.applyBindings != undefined) {
      $('.page-footer').applyBindings();
}

Hope that this may help some one else as well. For more details have a look at this link: https://codeblog.experius.nl/magento-2-uicomponent-reinit-ajax-reload/

  • Thanks @Narendra for posting answer, +1 for your answer. – Ramkishan Suthar Nov 1 '18 at 9:25
1

Try like

    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');
    });

OR

$('.page-footer').append(row).trigger('contentUpdated');
  • Tried but no luck :( not getting where I am wrong – Narendra Vyas Nov 1 '18 at 6:35
  • Did you get any response into response.categoryProducts? – Hitesh Nov 1 '18 at 6:37
  • Yes I am getting whole Html in its response, and all html content is displaying correctly accept that x-magento-init block. And that script with x-magento-init is also I am getting in response, I have checked by console log whole data – Narendra Vyas Nov 1 '18 at 6:38
  • try this $('.page-footer').append(response.categoryProducts).trigger('contentUpdated'); In which file you ahve write this code? – Hitesh Nov 1 '18 at 6:38
  • Actually we have used Amasty extension for shop by ajax, so I am writing this amasty extension js file i.e. amShopbyAjax.js – Narendra Vyas Nov 1 '18 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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