PDA

View Full Version : [CLOSED]Override doesn't work with singletons



firefoxSafari
19 Feb 2011, 6:43 PM
I'm not 100% sure if this is a bug or intended behavior, but I found it really annoying so I'm posting it.

If you define a class as a singleton, then the override method on the class is not present. For example, this fails with an error




Ext.define('Ext.ux.TestClass', {
singleton: true
});

Ext.ux.TestClass.override({
testMethod: function() {
alert('hi');
}
});


Also, Ext.override will just silently fail.



Ext.override(Ext.ux.TestClass, {
testMethod: function() {
alert('hi');
}
});


Now I realize this will work after the class is defined.



Ext.ux.TestClass.testMethod = function() {
alert('hi');
};


However, it would be nice to have a unified model for overriding and I can see this tripping a lot of people up, especially if they try to override internal ext classes to work around problems. I ran into it trying to alter TableChunker to let me select text in a grid.

Is this a bug or intended behavior?

Animal
20 Feb 2011, 7:15 AM
Override sets properties in a class's prototype

A singleton is just an object with properties.

firefoxSafari
20 Feb 2011, 9:45 AM
Thanks for the info - I'll assume this is expected behavior, then.

I understand how it's working now - setting the object property rather than touching the prototype was what my last snippet did. However, it seemed to me with the new Ext.define syntax you tried to present a unified way to create the definition for classes, both instantiable and ones intended to be singletons. Creating one intended to be singleton just requires a different config attribute. Given this, it seemed counterintuitive to me that the override mechanism wasn't smart enough to figure out what I meant when I tried to override a method on a singleton - it seemed there was a disconnect between define and override.

This tripped me up because just looking at TableChunker it looked like a regular class. Not a huge deal either way; we'll just have to condition ourselves to look for the singleton config option.

VDP
21 Mar 2013, 7:06 AM
It would be nice if I could override properties on a singleton too.

Then I can use the default sencha way of requiring an override. Now I do an empty override (doing nothing but not tripping any warnings). And just merge the properties..

In a file: TestClass


Ext.define('TestClass', { singleton: true});

In a file: overrides/TestClass


Ext.define('Ext.overrides.TestClass', {
override: 'TestClass'
});
Ext.merge(TestClass, {
propA: 'valA',
probB: function(){ console.log('Bam!'); }
});