I have two "modules" PanelManager and Panel which I want to be as independent as possible so I use events for communication between them. This way you can run each module independently on if the other one is running and you can plug or unplug modules dinamically just setting or removing the listeners.
The PanelManager module dispatches a newPanel event whenever it needs a new panel, and listens for newPanelCreated events.
The Panel module listens for the newPanel events, creates a new Panel and dispatches a newPanelCreated custom event with the new panel as {detail}.
To achive this I can design the Panel module in two different ways: the first is a PanelFactory class that sets the listeners when instantiated and creates instances of a Panel class. The second is just one Panel class with a flag in the constructor so if it's instantiated with a true flag it just sets the listeners, but if instantiated with a false flag it acts as a regular Panel. This way the same class could act as Factory and Panel at the same time.
Which approach would you recommend? Is there a better way to achieve this?
Aucun commentaire:
Enregistrer un commentaire