1. #1
    Sencha User
    Join Date
    Mar 2010
    Location
    Seattle, WA
    Posts
    137
    Vote Rating
    1
    Answers
    2
    wprater is on a distinguished road

      0  

    Default 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.

    Code:
    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);
      }...
    I'd like to have the getter, setter, apply, etc. for setting1 and setting2

    Ideas?

  2. #2
    Sencha - Services Team AndreaCammarata's Avatar
    Join Date
    Jun 2009
    Posts
    1,395
    Vote Rating
    23
    Answers
    148
    AndreaCammarata has a spectacular aura about AndreaCammarata has a spectacular aura about

      0  

    Default


    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:

    Code:
    
    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];
        }
    });
    In this way you can call

    Code:
    <CLASS>.getSetting('orientation');                        //Returns 'First Setting'
    <CLASS>.setSetting('orientation', 'New Value');
    <CLASS>.getSetting('orientation');                        //Returns 'New Value'
    However, if you want to to have the custom getter and setters function you can 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];
        }
    
    
    });
    And write:

    Code:
    <CLASS>.getOrientation();                                    //Returns 'First Setting'
    <CLASS>.setOrientation('New Value');
    <CLASS>.getOrientation();                                    //Returns 'New Value'
    or

    Code:
    <CLASS>.getScreenSize();                                    //Returns 'Second Setting'
    <CLASS>.setScreenSize('New Size');
    <CLASS>.getScreenSize();                                    //Returns 'New Size'
    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.
    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
    Owner at SIMACS

    @AndreaCammarata
    www.andreacammarata.com
    github: https://github.com/AndreaCammarata

    TUX components bundle for Sencha Touch 2.x.x
    https://github.com/AndreaCammarata/TUX


  3. #3
    Sencha User
    Join Date
    Mar 2010
    Location
    Seattle, WA
    Posts
    137
    Vote Rating
    1
    Answers
    2
    wprater is on a distinguished road

      0  

    Default


    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!

  4. #4
    Sencha User
    Join Date
    Mar 2010
    Location
    Seattle, WA
    Posts
    137
    Vote Rating
    1
    Answers
    2
    wprater is on a distinguished road

      0  

    Default


    Any thoughts on how to dynamically add getters/setters/etc. to a class at runtime?

  5. #5
    Sencha User
    Join Date
    Oct 2009
    Location
    Los Angeles, CA
    Posts
    29
    Vote Rating
    0
    gh0st26 is on a distinguished road

      0  

    Default


    I actually had a very similar requirement! here's a basic example of what I did:

    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);
    	}
    });
    and then create the class like this:

    Code:
    Ext.create("someClass", {
        .......
        propconfig: {
            ......
        }
    });
    I hope this helps!

  6. #6
    Sencha Premium Member dawesi's Avatar
    Join Date
    Mar 2007
    Location
    Melbourne, Australia (aka GMT+10)
    Posts
    1,083
    Vote Rating
    44
    Answers
    24
    dawesi has a spectacular aura about dawesi has a spectacular aura about

      0  

    Default


    Can you merge them further up the tree perhaps (untested)

    Code:
    Ext.define('someClass', {
         config: Ext.Object.merge({}, this.defaultConfig, this.deviceSettings)
    Other thought..

    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.Profile
    Teahouse Training Company
    Official Certified Sencha Trainer

    Australia / New Zealand / Singapore / Hong Kong & APAC



    SenchaWorld.com - Sencha webinars, videos, etc
    SenchaForge.org - (coming soon)
    TeahouseHQ.com - Sencha ecosystem training portal

    Code Validation : JSLint | JSONLint | JSONPLint