PDA

View Full Version : Singleton constructor arguments



tm8747a
5 Oct 2012, 6:11 AM
I'm having a hard time figuring out how I can define a singleton class and provide arguments for when it's instantiated. Take, for example, the following class:



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

constructor: function(config) {
this.val = config.val;
}

getVal: function() {
return this.val;
}
});


I can't do Ext.create('MyApp.Util', { val: 1 }); because the singleton class can't be instantiated that way. So how can I create a singleton passing it some values?

Tim Toady
5 Oct 2012, 6:20 AM
You don't call Ext.create with a singleton. It gets created when it is defined. You would just set it in the definition.

redraid
5 Oct 2012, 6:23 AM
Ext.define('MySingleton', {
singleton: true,
config: {
cfg1: 'def value'
},
constructor: function(cfg) {
this.initConfig(cfg);
}
});

// Usage
MySingleton.getCfg1(); // return 'def value'
MySingleton.setCfg1('1'); // set cfg1
MySingleton.getCfg1(); // return '1'

scottmartin
5 Oct 2012, 6:29 AM
To elaborate on this ...

config: { .. } // using config auto creates set/get

Scott

tm8747a
5 Oct 2012, 6:38 AM
Thanks for the answers. Problem is, all assume that I'd specify the data within the definition file itself. Unfortunately, the data I want to pass into the singleton comes from an external source. I suppose I could always create a null config value and then call MyApp.Util.setVal() before running other operations. Just thought it was cleaner if the Singleton got created with the proper data in it from the beginning.

Tim Toady
5 Oct 2012, 7:04 AM
These two statements seem contradictory to me since the definition is the creation with a singleton.

"Problem is, all assume that I'd specify the data within the definition file itself"

"Just thought it was cleaner if the Singleton got created with the proper data in it from the beginning"

Technically, a singleton is just an object so you can set the property whenever you want. You can set it later or grab it when you define it. You can grab it from the external source in the singletons constructor. Might be a little funky with something like Ajax though since you would have to put in a callback to set it. Personally I would try to have it available when the singleton gets defined. If I need something from the server on page load I sometimes just have my index stick the value in a variable to grab later. It is hard to guide you without more specific information.

tm8747a
5 Oct 2012, 8:40 AM
These two statements seem contradictory to me since the definition is the creation with a singleton.

"Problem is, all assume that I'd specify the data within the definition file itself"

"Just thought it was cleaner if the Singleton got created with the proper data in it from the beginning"

Technically, a singleton is just an object so you can set the property whenever you want. You can set it later or grab it when you define it. You can grab it from the external source in the singletons constructor. Might be a little funky with something like Ajax though since you would have to put in a callback to set it. Personally I would try to have it available when the singleton gets defined. If I need something from the server on page load I sometimes just have my index stick the value in a variable to grab later. It is hard to guide you without more specific information.

I think my use of dependency injection in my server-side language got me confused about the whole singleton pattern. With DI my classes only get instantiated once, but the DI framework is the one making sure you only get one instance, whereas the class you are retrieving makes no use of the singleton pattern, it's just defined as a normal class. Thanks for helping me connect the dots on this. Guess I just need to revisit my approach and either use some form of setter to set the data I need or simply not use a singleton.

arcsight-gui
4 Sep 2015, 8:27 PM
Ext.define('MySingleton', {
singleton: true,
config: {
cfg1: 'def value'
},
constructor: function(cfg) {
this.initConfig(cfg);
}
});

// Usage
MySingleton.getCfg1(); // return 'def value'
MySingleton.setCfg1('1'); // set cfg1
MySingleton.getCfg1(); // return '1'


No. There is no such global variable available. Add this class to the requires of any other class and try to use it in a method as a global variable like this ... you get an error saying it is undefined.

Please describe how you can successfully access this kind of "singleton".

Tim Toady
8 Sep 2015, 5:26 AM
No. There is no such global variable available. Add this class to the requires of any other class and try to use it in a method as a global variable like this ... you get an error saying it is undefined.

Please describe how you can successfully access this kind of "singleton".

You can do it successfully exactly as stated. You are doing something wrong. Open a new thread describing your problem rather than reviving a long dead one.