0
votes

I am having a hard time overriding a constructor from a singleton class in ExtJS 4.1. I have an override defined, but by the time my override statement gets processed, the constructor has already executed.

Ext.define('singleton', {
    singleton: true,

    constructor: function() {
        alert('replace me');
    }
});
2

2 Answers

1
votes

Overriding the constructor of a singleton doesn't make sense, a singleton is a class that is very early on in the execution of Extjs turned into an instance of itself. This means that you are trying to override an instance of a class and not the class itself.

I can recommend that whatever you want done to the singleton be done in a separate method in the class definition that you can call very early in Ext.onReady() or your apps instantiation.

-1
votes

You can't override a singleton class, that is true, but you can override the singleton instance:

Ext.define('singleton', {
    singleton: true,

    constructor: function() {
        // does whatever
    }
});

Ext.define('singletonOverride', {
    override: 'singleton',

    // adding new property
    foo: 'bar',

    // adding new method
    baz: function() {},

    initSingletonOverride: function() {
        // do whatever is needed
        // to augment the singleton
        // behavior the way you want
    }
},
function() {
    // `this` is the singleton instance
    this.initSingletonOverride();
});

Keep in mind that all this fluff comes down to the JavaScript objects. A class is an Object, and a class instance is another Object. You can override either of them, or both; the class system is here to help you not to lose track of what you're doing.