如何挂接到 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);
});
我有一个 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);
});