April 07, 2014

Delegating Event handlers to align Event Data model

After such a pretentious title we can all stop and take a one week sleep. WTF do I want from you now, you probably ask. yeah, ok. check this out:

Think about this next situation - You have a nice component which potentially can host several other components, each can dispatch a different event, an event that needs to be handled of within the "parent" component.
We cannot let the "parent" component manage all the events potentially fired from its nested components, right? I mean, we can let it but then we will pretty much shit into our own development experience and maintenance, after all, we want the "parent" component to be aware of a single Event data model.

Being aware of a single event data model means that the "parent" component will expect a single format of data when receiving events from any of its nested components. It will not matter what's the data model of each nested component, something will have to align it into a single well-known model. The question is who?

We need some kind of a delegator, which will know all the potential events and will process their data and send a nice formatted data back to the "parent" component.
So, if we are to drill into more details, we're actually talking about extending the "parent" component's Scope, since it is that Scope which will eventually listen to all these events, but it will be done in a sort of a "mixin" approach.
The solution I came up with introduces a service which has a single method registering the "parent" component's scope. From there on, the service will extend the Scope and eventually send an event back to the "Parent" component with the processed event data.
Simple does it.
The POC can be found in Plunker

I would love to hear if you have any comments about it.

No comments: