简体   繁体   中英

Extjs3.4 How to override the constructor

I have a need to override native constructor of one of Extjs classes. If to be more exact Ext.ensible.cal.DragZone, it is from extensible calendar.

What I have tried, and what solutions I have found

  1. Put desired code to original code, but I want to avoid original code modifications. All my changes I want to keep in one place

  2. Ext.override - it doesn't work. native constructor is called anyway

  3. Fully duplicate the code from original library, but with my changes. Ext.ensible.cal.DragZone = Ext.extend(Ext.dd.DragZone, {..... }); But this causes some other unexpected error, seems that not all functionality is called(some of fields of the class, seems not to be properly initialized). But if I just put this code, instead of original, everything works fine(but see #1)

  4. Create my own Ext.ensible.cal.DragZone by extending the original. Problem of this method, is that I need to review all the code of library, and override and/or extend all other classes which uses this DragZone, to make them to use mine.

So can you advise me smth. The most correct solution for me seems to be just constructor overriding.

How I overriding:

Ext.override(Ext.ensible.cal.DragZone, {
    constructor : function () {
        ...
    }
});

But as I told, original constructor is called

Here's a hack you can try, remember that the class name is the constructor, there is actually no constructor property on the class that is generated

Ext.ensible.cal.DragZone = function () {
    // This is not very nice, it'd be better if you didn't have to 
    // assume what the parent is, but since we're already hacking it...
    Ext.dd.DragZone.apply(this, arguments) ;
    // Your code here
}

Note that this is not the safe approach as I mentioned in my comment, but should work

I suggest you use approach 4 so that your modifications only apply to classes that choose to use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM