Here is the presentation on how to get started on using
StarShine.
I'm going to demonstrate now how to actually make an app structured on top of StarShine.
This
_eventify
method will turn the private namespace of your module into an event emitter. This is useful for many things, but mostly it's a building block for an event emitter Factory. In order to expose the event factory, we simply
return this;
to expose the methods to the object. Finally, we wrap it in a StarShine factory. Here are some rules to remember.
- If the parts you create should remain private, create a closure.
- If the parts you create should only be exposed and the rest of your closures don't rely on it, create a compound factory
- If the parts you create should be BOTH, create a factory and create an instance in your closure
In this case, I want the event emitter to be public AND private. Option three is our target.
Of course, if you wanted events to be private (and therefore not exposed,) simply include the original function that created the event emitter in the first place. You don't even need the
_exposeThis
function.
Finally, if I want the methods to simply be exposed without referenced internally, I can do factory re-use.
Moving on, we should create a couple of closures that define different portions of our application. I'm going to look for anything with a [data-user-control-1] attribute on my dom and put a link inside of it.
This is a lot of code to read, so take your time and re-read it.
On the highest level, we define a process. Inside this process, we define a factory for creating a dom fragment and inject it into the container AND the private namespace for later use inside of the
_createFragment
function.
Then we create an anchor tag using the terrible dom api (encapsulated and transparent to the API consumer!) and append it to the container defined already.
Then we add a click listener to the anchor in a separate process that triggers a global APP event. The
this
in the parent closure is a reference to the event emitter exposed by our app.
We define an API for the control that can be consumed by the app and anyone who has access to see the UserControl. I put a remove function into the definition of a "UserControl1" as an example.
Finally, I add an event listener to the app to listen for UserControlClick events and remove the anchor from the DOM.
If you define your Application in terms of functions, suddenly everything is modularized and is much easier to maintain. Embrace the "encapsulate and forget about it" principle because you are making building blocks and tools.
The following example is a good place to start for your app.
Experiment and make the code work for you.
Now, onto factory inheritence (as promised!)
If you must accept different parameters, or some kind of state needs to be shared between the factories, maybe it would be best to make the combined factories actual private children of the parent factory. Each factory should be a unit or an encapsulated building block designed to operate all by itself.