1

I wonder if anyone can help

I have a jQuery plugin with the following code:

....

$.Autocompleter.Select = function (options, input, select, config) {
    var list;

    ....

    return {
        emptyList: function (){
            list && list.empty();
        }
    }
}

I would like to override the emptyList function; and have written the following code:

originalAutoCompleteSelectFunction = $.Autocompleter.Select;
$.Autocompleter.Select = function (options, input, select, config) {
    var ret = originalAutoCompleteSelectFunction.apply(this, arguments);

    ret.emptyList = function() {
        console.log ("hello");
        console.log ("The list=" + list);
    };

    return ret;
};

It sort of works, but:

  1. My implementation of emptyList runs - I see 'hello' on the console - but the orignal implementation of emptyList also runs. So its not really an overridden function is it? Its more like an augmentation?
  2. I dont seem to be able to access the variable 'list'

So, 2 questions. Have I written the code correctly to truely override the existing function? And, is it possible to access the variable 'list'? I know its a private member of the closure, but if you cant access private members in this way presumably it makes the ability to override functions (that use those private members) pointless ?

Or have I missed something?

I've already looked at Extending an existing jQuery function which gave me the steer on how to override functions in this way; and also this Access private members of jQuery plugin which suggests that accessing private members of a closure is not possible.

Cheers

Nathan

Community
  • 1
  • 1
Nathan Russell
  • 3,428
  • 5
  • 30
  • 51
  • I've tried console.log ("The list=" + this.list); but that didnt work – Nathan Russell May 17 '12 at 13:03
  • And I've also tried creating a getter function: ret.getList = function() { return this.list; } Whilst I could call the getter, it in turn still could not access the 'list' variable – Nathan Russell May 17 '12 at 13:04
  • It may not be possible. Perhaps just alter the plugin itself. Or, check if it provides callback function options that will allow you to do whatever it is you're trying to do. – ScottE May 17 '12 at 13:06
  • I really dont want to alter the plugin source - that seems bad (would you want someone hacking your code?, and if they did would you support them? (not that we get support for this plugin)). My first thoughts were around call backs, but I dont think its been written that way, hence wanting to override some functions – Nathan Russell May 17 '12 at 13:41
  • Is the plugin on github? If so you can fork and submit a pull request. – blockhead May 17 '12 at 13:52
  • @Nathan, if you want to change private members, then you're doing something that the plugin wasn't designed for anyway. – ScottE May 17 '12 at 15:06
  • 1
    @ScottE - yes, thats true; but the same could be said about any kind of overriding, whether I access private members or not. Overriding a function/method/whatever is presumably because the base functionality doesnt do quite what I now want it to ? – Nathan Russell May 17 '12 at 15:19

0 Answers0