tester
tester

Reputation: 3987

How to hook into a jQuery widget's function?

I have a jQuery widget (off topic: it is from Magento 2) which has a function which looks like this:

$.widget('custom.SwatchRendererTooltip', {
    // some more code here....

    /**
     * Update total price
     *
     * @private
     */
    _UpdatePrice: function () {
        var $widget = this,
            $product = $widget.element.parents($widget.options.selectorProduct),
            $productPrice = $product.find(this.options.selectorProductPrice),
            options = _.object(_.keys($widget.optionsMap), {}),
            result;

        $widget.element.find('.' + $widget.options.classes.attributeClass + '[option-selected]').each(function () {
            var attributeId = $(this).attr('attribute-id'),
                selectedOptionId = $(this).attr('option-selected');

            options[attributeId] = selectedOptionId;
        });

        result = $widget.options.jsonConfig.optionPrices[_.findKey($widget.options.jsonConfig.index, options)];

        $productPrice.trigger(
            'updatePrice',
            {
                'prices': $widget._getPrices(result, $productPrice.priceBox('option').prices)
            }
        );

    },
}

I want to hook into this function somehow or at least want to know when it is triggered. So whenever _UpdatePrice is called I want to know it in order to do things. How is that possible?

Upvotes: 2

Views: 1039

Answers (1)

Rudi
Rudi

Reputation: 2995

The function triggers an event updatePrice that you can listen to:

$('body').on('updatePrice', function (e, data) {
  console.log(e.currentTarget, data.prices);
});

Upvotes: 2

Related Questions