TransWikia.com

How to call a js method in my template with RequireJS in Magento2?

Magento Asked on December 26, 2021

In vendor/magento/module-catalog/view/base/web/js/price-options.js there is a method _onOptionChanged: function onOptionChanged(event).

I would like to call this method in my template when page load.

app/design/frontend/Smartwave/porto/Magento_Catalog/templates/product/view/options/wrapper.phtml

How can i achieve this? is it possible to call this method in my template file ? if possible please help me and suggest me how can do it?

One Answer

You can't do it in template, and you can't access directly to a private method. You can do it in a requireJS module through the mixins configuration.

See the docs.

In your case you could do something like his:

var config = {
    'config':{
        'mixins': {
            'priceOptions': {
                'Vendor_Module/script' : true
            }
        }
    }
}; 

And in your script.js

define(['jquery'], function ($) {

    return function (widget) {

       $.widget('mage.priceOptions', widget, {

           yourMethod: function () {
                 this._onOptionChanged
           }

       });
       return $.mage.priceOptions;
   }
});

Answered by nuovecode on December 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP