【问题标题】:How to hook into a jQuery widget's function?如何挂钩到 jQuery 小部件的功能?
【发布时间】:2016-05-26 19:18:25
【问题描述】:

我有一个 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 时,我都想知道它以便做事。这怎么可能?

【问题讨论】:

    标签: javascript jquery jquery-ui magento widget


    【解决方案1】:

    函数触发一个事件updatePrice,你可以收听:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 2011-06-02
      • 2023-03-16
      相关资源
      最近更新 更多