Question

I make plugin:

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

later I try use this select as:

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

Then replace this select I can't see messages ...log("Autocomplete sel... My function "sel" not work after this. How can i write my select what uset in plugin and not crash then it replaced by external code?

in code:

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

    options: {
        id:""
    },

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

"select" is not work then selecting element in dropdown

Was it helpful?

Solution

$.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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top