-
31 Jan 2012 2:17 PM #1
Unanswered: Create Class Getters and Setters in Constructor
Unanswered: Create Class Getters and Setters in Constructor
I need to create a set of getters and setters in the constructor. I can't have them in the config property.
Tried a handful of scenarios, but none of them seem to be working yet. Hoping someone can help.
Here is one attempt, trying to merge the default config object from another instance property before calling initConfig. This is not working.
I'd like to have the getter, setter, apply, etc. for setting1 and setting2Code:Ext.define('TestClass', { config: { foo: 'bar' }, deviceSettings: { setting1: null, setting2: null }, constructor: function(config) { this.defaultConfig = Ext.Object.merge({}, this.defaultConfig, this.deviceSettings); this.initConfig(config); }...
Ideas?
-
1 Feb 2012 5:02 AM #2
Why can't you put them inside the Config object?
Is this because these methods should be private and not accessible outside the class?
However, I don't think you need a getter and setter for each the settings.
You could just create a single get / set function:
In this way you can callCode:Ext.define('TestClass', { config: { foo: 'bar' }, deviceSettings: { orientation: 'First Setting', screenSize: 'Second Setting' }, setSetting: function(setting, value){ return this.deviceSettings[setting] = value; }, getSetting: function(setting){ return this.deviceSettings[setting]; } });
However, if you want to to have the custom getter and setters function you can writeCode:<CLASS>.getSetting('orientation'); //Returns 'First Setting' <CLASS>.setSetting('orientation', 'New Value'); <CLASS>.getSetting('orientation'); //Returns 'New Value'
And write:Code:Ext.define('TestClass', { config: { foo: 'bar' }, deviceSettings: { orientation: 'First Setting', screenSize: 'Second Setting' }, constructor: function(config) { var me = this, cap = Ext.String.capitalize; me.initConfig(config); for(setting in me.deviceSettings){ me['get' + cap(setting)] = Ext.pass(me.getSetting, [setting], me); me['set' + cap(setting)] = Ext.pass(me.setSetting, [setting], me); } }, setSetting: function(setting, value){ return this.deviceSettings[setting] = value; }, getSetting: function(setting){ return this.deviceSettings[setting]; } });
orCode:<CLASS>.getOrientation(); //Returns 'First Setting' <CLASS>.setOrientation('New Value'); <CLASS>.getOrientation(); //Returns 'New Value'
However, I think that all this is just a complication of what the ClassSytem already do, because, even in this case, this functions will be public in the same way.Code:<CLASS>.getScreenSize(); //Returns 'Second Setting' <CLASS>.setScreenSize('New Size'); <CLASS>.getScreenSize(); //Returns 'New Size'
In addition, putting this settings inside the config object, will allow you to use the "update<Setting>" function, which will allow you to handle your class in a better way.
Hope this helps.Sencha Inc
Andrea Cammarata, Solutions Engineer
CEO at SIMACS
@AndreaCammarata
www.andreacammarata.com
github: https://github.com/AndreaCammarata
-
1 Feb 2012 10:58 AM #3
Thanks for your response, but this is not what I'd like. I don't mind if they are accessible as public members, but goal is to know which settings are being set and have getter/setter/applier for each of them. If I were to put them in the config, then there is no way to iterate over them later (which I need to do), unless there were no other properties in the config (which very well won't be the case).
It seems that the defaultConfig has the getters/setters/appliers created in the instantiation of the class, but not in the constructor? I've looked throughout the code and tried a handful of overrides but cannot seem to reproduce this in my overridden constructor.
Can you shed some light as to how these getters/setters/appliers are created when the class is instantiated and then maybe I can add this functionality for arbitrary config members that are added at a later time in the objects lifecycle.
Thanks!
-
2 Mar 2012 10:59 AM #4
Any thoughts on how to dynamically add getters/setters/etc. to a class at runtime?
-
5 Apr 2012 4:13 PM #5
I actually had a very similar requirement! here's a basic example of what I did:
and then create the class like this:Code:Ext.define('someClass', { config: { propconfig: {}, props: [] }, constructor: function(config){ // Build the list of prop keys for later access. for (prop in config.propconfig){ this.config.props.push(prop); } config.props = this.config.props; // Build the data to pass to the preprocessor var data = { config: config.propconfig, preprocessors: ['config'] }; // Call the preprocessor Ext.Class.process(this.self, data); // Delete the propconfig obj from the config // no use in having duplicates delete config.propconfig; // Init the config this.initConfig(config); } });
I hope this helps!Code:Ext.create("someClass", { ....... propconfig: { ...... } });
-
13 Nov 2012 4:45 PM #6Sencha - Sales Team
- Join Date
- Mar 2007
- Location
- Melbourne, Australia (aka GMT+10)
- Posts
- 738
- Vote Rating
- 6
- Answers
- 10
Can you merge them further up the tree perhaps (untested)
Other thought..Code:Ext.define('someClass', { config: Ext.Object.merge({}, this.defaultConfig, this.deviceSettings)
Are you basing these settings on different devices, then can you use profiles for what you are doing or is your requirement more granular that this?
http://docs.sencha.com/touch/2-0/#!/api/Ext.app.ProfileCheck out SenchaWorld.com for articles, screencasts, conference videos and more.
Sencha Technical Training : Asia Pacific Region
Code Validation : JSLint | JSONLint | JSONPLint


Reply With Quote


