life
The life plugin tracks a spirits lifecycle. Oftentimes a spirit would use it to retrieve the lifecycle status of another spirit.
var otherspirit = gui.get('#thatspirit');
console.log(otherspirit.life.attached); // true
Lifecycle flags
The lifecycle status of a spirit is reflected in a set of boolean properties. These are all readonly, or at least they should be.
constructed- Spirit is constructed? This is almost certainly true by the time you address the spirit.
configured- Spirit is configured? If true, the spirits (other) plugins are ready to be used.
entered- The spirit is now or has ever been positioned in the DOM?
attached- The spirit is currently positioned in the DOM? Note that this is initially
falseuntil the spirit has been discovered by the framework. It is alsofalsewheneverdetachedistrue. detached- The spirit is currently not positioned in the DOM? Note that this is initially
trueuntil the spirit has been discovered by the framework. It is alsotruewheneverattachedis false. ready- The method
onreadyhas been called on the spirit? Iftrue, this implies that all descendant spirits are alsoready. async- The spirit has entered the phase of it's life that happens roughly 4 milliseconds after
ready? destructed- The spirit is destructed? If true, don't try anything funny. The spirits properties and (other) plugins may not even exist.
Perhaps refactor something to allow undefined as a third state for these booleans?
Lifecycle listeners
You can add like an event listener to get notified whenever a spirit transitions into a new lifecycle phase. This is particularly useful for plugins, as they would otherwise need to rewrite the spirits methods.
onconstruct: function() {
this.super.onconstruct();
this.spirit.life.add(gui.LIFE_ATTACH);
}
onlife: function(l) {
if(l.type === gui.LIFE_ATTACH) {
this.spirit.dom.parent().className = "container";
}
}
The callback method recieves a gui.Life object with two properties type and target where the target is always a spirit. Note that this is a "non-bubbling" type of event, so you need to
get a hold of the spirit in order to listen for it. We recomment you add listeners using this list of constants.
gui.LIFE_CONSTRUCTgui.LIFE_CONFIGUREgui.LIFE_ENTERgui.LIFE_ATTACH
gui.LIFE_READYgui.LIFE_DETACHgui.LIFE_ASYNCgui.LIFE_DESTRUCT
The constants map down to strings, but we can reduce the risk of typos by using the constants instead of the strings. That's because the plugin has been rigged to explode on undefined.
Custom events
You can make up new lifecycle events as you go along.
this.life.copypasted = true;
this.life.dispatch('copypasted');