Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending YUI3 Plugins and Classes

I am using YUI3's Auto-complete Plugin. Y.Plugin.Autocomplete.

Now I want to extend this plugin and create some very specific plugins. Such as Y.MyModule.TagAutocomplete, Y.MyModule.EmailAutocomplete and so on.

My simple question is, when I am writing initializer method in my subclass do I need to call superclass constructor explicitly or does it happen implicitly ? If I have to call it what is the syntax ?

like image 410
Eastern Monk Avatar asked Nov 21 '25 12:11

Eastern Monk


1 Answers

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

In details:

  1. You create a "constructor function". Here you should call superclass constructor:

    function MyClass(config) {
        // Invoke Base constructor, passing through arguments
        MyClass.superclass.constructor.apply(this, arguments);
    }
    
  2. Next, use Y.extend method to make your own class extended from Y.Base (or Y.Plugin.Autocomplete in your case)

    Y.extend(MyClass, Y.Base, {
        // Prototype methods for your new class
    });
    
  3. Y.Base has a special method called "initializer" - this method executed on each class in hierarcy when you create a new object and you do not need to call parent's initizlizer manually. I think Y.Plugin.Autocomplete has its own "initializer". So jus do following:

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

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

like image 175
Petr Avatar answered Nov 24 '25 01:11

Petr