widget如何替换jqueryUI中的父选择

时间:2014-04-21 05:18:16

标签: jquery jquery-ui inheritance widget

我制作插件:

$.widget("my.idautocomplete", $.ui.autocomplete, {

    options: {
        oldselect:{},
        id:""
    },
    _create: function () {
        $.ui.autocomplete.prototype._create.call(this);

        this.options.oldselect=this.options.select;
        this.options.select=this.sel;
    },

    sel:function (event, ui) {
        console.log("Autocomplete sel " + $(this).attr("id"));
    }
}

稍后我尝试将此选择用作:

$("input").idautocomplete({select:function(){alert(1);} });

然后替换此选择我看不到消息... log(“Autocomplete sel ...我的功能”sel“在此之后不起作用。 我如何编写我的选择在插件中使用什么uset而不是崩溃然后它被外部代码替换?

代码:

    $.widget("my.idautocomplete", $.ui.autocomplete, {

    options: {
        id:""
    },

    select:function (event, ui) {
        console.log("Autocomplete sel " + $(this).attr("id"));
    }
}

“选择”不起作用然后在下拉列表中选择元素

1 个答案:

答案 0 :(得分:0)

$.widget("my.idautocomplete", $.ui.autocomplete, {

    options: {
           oldselect:undefined,
           select:function sel_(event, ui) {
                    var th=this;
                     //some my plugin code
                     ...
                      // call select added from HTML programmer
                    $(th).idautocomplete("sel",event, ui);
            }
    },
    sel:function oldSel(event, ui) {
       var th=this;

        // if has external added select, call it
       var oldSelect = th.options.oldselect;
        if(oldSelect!==undefined){
            oldSelect.call(th,event, ui);
        }
    },

    _setOption: function () {
        if (arguments[0]=="select") {
           //getting a new onSelect function and save it in options
            this.options.oldselect=arguments[1];
             //save private plugin select
            var tst=this.options.select;
             // call _setOption to correct initialisation autocomplete
            $.ui.autocomplete.prototype._setOption.apply(this, arguments);
             // restore private select as default
            this.options.select=tst;
        }else{
            $.ui.autocomplete.prototype._setOption.apply(this, arguments);
        }
    },

.....

})(jQuery, window, document);