PDA

View Full Version : How to tell if setXXX is being called in initConfig



walter.rumsby
28 May 2013, 8:54 PM
I've been playing around with config and I like features like automatically created getters and setters, update & apply methods, etc.

I want to be able to tell if a call to setXXX, applyXXX and updateXXX is happening in the "init phase" (i.e. as a consequence of this.initConfig being called) or at some point later in the lifecycle.

Because Ext.Base is fairly lightweight there doesn't seem to be any properties that I can inspect to tell if "construction" of an object has finished (at least none that I could see). Other techniques I've played around with are:

walking up the call stack to see if initConfig is in the stack (works, strict mode wouldn't be happy, feels wrong)
creating interceptors around updateXXX methods and detecting if oldValue === undefined (would fail if someone subsequently set the value to undefined then set it to something else; also assumes I always want to cancel the update during the init phase)
adding my own property to the class that indicates the construction has finished (works, but meh)
I like what config gives me (largely because it looks a bit like YUI's Attribute) and I'm trying to find a lightweight way of detecting "I'm initialising things here, don't worry about this".

Relevant jsFiddle:

http://jsfiddle.net/wrumsby/3UfZm/


(http://jsfiddle.net/wrumsby/3UfZm/)

slemmon
30 May 2013, 10:35 AM
I'm not seeing a public property to key off of specifically. But, if you're extending Component you might look to see if this.id exists as it's not yet set by the time your initial setters are run.

If you're extending Base you will probably have to go with the setting of a property during construction and then see if it exists when running applyFoo (your third bullet).

walter.rumsby
30 May 2013, 11:44 AM
I can't tell for sure, but it appears that undefined is a special value here, i.e.

* oldValue === undefined in the init phase
* if applyFoo returns undefined then setFoo is cancelled - the property isn't updated and updateFoo isn't called

While that suggests that you can't set a config item to undefined later on it seems like a workable solution for objects that only inherit from Base or Observable.