aurajs / aura

A scalable, event-driven JavaScript architecture for developing component-based applications.
MIT License
2.94k stars 255 forks source link

Access to parent(super) methods within component #344

Open igor-tomov opened 10 years ago

igor-tomov commented 10 years ago

Hi,

I decide to implement my custom components behavior via components.addType(type, def). In fact, as I understood, an object, which I'm passing to this method will be prototype for all components with appropriate type. But there I faced with issue of getting access to prototype methods(super), which I override in my concrete component (in my case it concerns to initialize method).

For example, we have new component type:

components.addType( "custom", {
    initialize: function( opts ){
        // some basic initialization
    }
    ......
})

and then, I want to call it within of overridden method:

define({
    type: "custom",

    initialize: function( opts){
        // like this
        this.__super__.initialize.call( this, opts );

        // or maybe like in "Base2" or "jQuery UI Widget factory"
        this._super( opts );
    }
})

Of course, JavaScript doesn't support such mechanism for inheritance, but many famous libraries such as Base2 of Dean Edwards and jQuery UI Widget factory implemented workaround for accessing to super methods. In Component.extend(), which is used for component inheritance, I didn't find such mechanism.

Actually, I have found one way, but it looks awful:

this.constructor.__ super__.constructor.__super__[ "propertyName" ];

Though, we can register callback via components.before() for this situation. But, I think, it's not really good idea, because this is more common approach and used for all loaded components.

So. Is there some convenient way to resolve it? Sorry for my bad English :( I hope, I described it correctly.

josephspens commented 9 years ago

Maybe we try something like Backbone's initialize. Initialize is a noop, a function the developer can write themselves for any custom initialize functionality, and Backbone will use constructor under the hood. So a private method paired with a public, overridable noop for the developer.