PDA

View Full Version : How to extend a singleton?



Dumas
5 Feb 2013, 9:53 AM
How can I cleanly extend the Ext.data.Validations singleton?

The following code is not working, it returns a Sencha-internal error:

Ext.define('Bancha.data.override.Validations', {
override: 'Ext.data.Validations',
fileMessage: "is not a valid file",
file: function(config, value) { ... }
});
//error: Object [object Object] has no method 'override' at sencha-touch.js:6595

Thanks
Roland

Dumas
6 Feb 2013, 4:57 AM
Anyone?

winkelmann
6 Feb 2013, 5:06 AM
As far as I can tell, a singleton cannot be extended. Singletons in ExtJS aren't singletony like in other languages, but rather classes that get instantiated immediately after they are loaded overwriting the class definition/prototype that was originally specified in the class tree location.

So if you have a singleton:


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


what ExtJS basically does after loading, is:


MyApp.MySingleton = Ext.create('MyApp.MySingleton');

Dumas
6 Feb 2013, 7:08 AM
So the only thing I could do to get this working with the dynamic loading is this?


+
Ext.define('Bancha.data.override.Validations', {
constructor: function() {
// we don't want to construct anything, just extend the validation rules
Ext.apply(Ext.data.Validations, {
fileMessage: "is not a valid file",
file: function(config, value) { ... }
}); //eo apply
} //eo construct
});

Not the most elegant code.

Thanks
Roland

winkelmann
6 Feb 2013, 7:23 AM
I guess that would be one way to go about it. Extending singletons somehow feels wrong anyway though ;-)

Dumas
6 Feb 2013, 8:30 AM
Well, but that is the official way to get new validation rules.

vadimv
6 Feb 2013, 8:51 AM
I would do it like this:


Ext.data.validations.self.addMembers({
meow: function() {
alert('Meowww...');
}
});

GustavR
31 Oct 2013, 1:04 AM
Thanks to kostysh (http://www.sencha.com/forum/showthread.php?231375-how-to-extend-singleton&p=859426&viewfull=1#post859426) I found my solution as well, thought I share it.
How to extend ExtJs singleton class already defined by framework:

// set custom define so dependency manager is not confused by seemingly unknown Ext.util.History.self
// @define Ext.util.History.self
Ext.require('Ext.util.History', function() {
Ext.define('My.util.History', {
extend: 'Ext.util.History.self',
singleton: true,

startUp: function() {
// do some own stuff
this.callParent(arguments); // you can even use callParent
}
});
});