繁体   English   中英

如何挂钩 jQuery 小部件的功能?

[英]How to hook into a jQuery widget's function?

我有一个 jQuery 小部件(题外话:它来自 Magento 2),它的功能如下所示:

$.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)
            }
        );

    },
}

我想以某种方式连接到这个函数,或者至少想知道它何时被触发。 所以每当_UpdatePrice被调用时,我想知道它以便做事情。 这怎么可能?

该函数会触发一个您可以监听的事件updatePrice

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM