How to subclass a specific jqueryui widget method?

﹥>﹥吖頭↗ 提交于 2019-12-03 21:48:03

The datepicker in 1.8 doesn't use the widget factory. The other widgets do but the datepicker hasn't been refactored to use the widget factory yet, something that will happen for a later version of jQuery UI.

You can do something like for the datepicker:

var old_showDatepicker = $.datepicker._showDatepicker;
$.datepicker._showDatepicker = function(input){
    console.log('hello');
    old_showDatepicker.apply(this,arguments);
    console.log('goodbye');
}

And this for the slider:

$.widget("my.slider", $.ui.slider, {
    _value: function(input) {
        console.log('Yo');
        $.ui.slider.prototype.value.apply(this,arguments);
        console.log('Hey!');
    }
});

ui.tabs has gotten lots of love, an alternate perhaps less code way is this:

https://github.com/jquery/jquery-ui/blob/c1cda180a93a6c0a63cf21a68dacb54233e03d03/ui/jquery.ui.tabs.js#L688

(function( $, dp ){
    var _showDatepicker = dp._showDatepicker;

    dp._showDatepicker = function( input ){

        alert( 'Yo' );
        _showDatepicker.apply( this, arguments );
        alert( 'Hey!' );
    }
}( jQuery, jQuery.ui.datepicker );

TBH, I would not look at datepicker. The widget factory is far more advanced, just look at how ui.tabs hooks up events and methods at the same time here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!