Encapsulation
Enyo Scoping
Enyo kinds are meant to be self-contained, so that they may be reused in different scenarios. Encapsulation is a major goal shaping the way scoping works in Enyo.
In a kind definition, the list of components determines what is in scope for the kind. In other words, the this.$ hash for a kind contains the collection of components defined in the kind.
Code should not attempt to access components in another object's $ hash. For example, this.$.oneComponent.$.itsChild should be avoided, as it breaks encapsulation by assuming the presence of something (itsChild) that is not in the scope of the current kind.
Communication Between Objects
So how do you allow one object to communicate with another? In one of two ways: by calling methods or sending events.
Picture the UI as a series of controls nested within other controls.
- To communicate from the outer layers in, have the parent call an exposed method on the child, then repeat as necessary until you reach your destination. This behavior is required to maintain encapsulation. The moment a kind assumes the presence of a certain child more than one level away, encapsulation is broken, and that kind cannot be used outside of that particular situation.
- To communicate from the inside out, use events. (Events are explained in greater detail in a separate article.)
Globals
Globals are bad. There should be virtually no reason to have global variables in Enyo. If you find yourself using lots of globals, most likely you are not honoring encapsulation, and some number of your kinds are defined such that they are limited to one particular usage.