Extending YUI3 plugins and classes

I am using Auto-Complete plugin YUI3. Y.Plugin.Autocomplete.

Now I want to extend this plugin and create some very specific plugins. For example, Y.MyModule.TagAutocomplete, Y.MyModule.EmailAutocomplete, etc.

My simple question is, when I write an initialization method in my subclass, do I need to explicitly call the constructor of the superclass, or does this happen implicitly? Should I call this syntax?


source to share

1 answer

I've never tried to extend plugins, but I did extend from Y.Base and it works as described here: http://yuilibrary.com/yui/docs/base/

More details:

  • You are creating a "constructor function". Here you have to call the superclass constructor:

    function MyClass(config) {
        // Invoke Base constructor, passing through arguments
        MyClass.superclass.constructor.apply(this, arguments);

  • Then use the Y.extend method to make your own class extend from Y.Base (or Y.Plugin.Autocomplete in your case)

    Y.extend(MyClass, Y.Base, {
        // Prototype methods for your new class

  • Y.Base has a special method called "initializer" - this method is executed for each class in the hierarchy when you create a new object, and you do not need to manually call the parent initializer. I think Y.Plugin.Autocomplete has its own "initializer". Therefore, jus does the following:

    Y.extend(MyClass, Y.Plugin.Autocomplete, {
        initializer: function(config) {
            alert("This initializer called after Y.Plugin.Autocomplete initializer");

Last comment from my side: I've never tried to extend Y.Plugin.Autocomplete, I have something under the hood in my Autocomplete implementation. Try it!



All Articles