jFactory > Reference > Traits > TraitState
TraitState
Provides the Proxy $.states
that triggers Component Events on property changes.
The states can be changed directly by assignation, or through $state()
which allows
to wait for asynchronous event handlers.
The “enabled” and “installed” Service States are accessible in this object.
Registry
myComponent.$.states
Injected Methods
$state(key {string}, val {*}, notify = true {boolean})]
Returns: JFactoryPromiseSync
Sets or changes a state and, if notify
is true (optional default), trigger beforeStateChange
and afterStateChange
.
Using this method instead of just setting the value in myComponent.$.states
allows to await for the completion of asynchronous handlers.
myComponent.$on("beforeStateChange", (event, eventData) => {
this.$log(eventData.key, eventData.previousVal+' => '+eventData.val)
});
myComponent.$on("afterStateChange", (event, eventData) => {
this.$log(eventData.key, eventData.previousVal+' => '+eventData.val)
});
await myComponent.$state("myState", "1234")