mardi 21 février 2017

Design pattern for Parent child relation

What is the best design pattern for Parent child relationship in Javascript?

  1. Parent / child can live on its own.
  2. Is it better to inject parent on every child and use pub/sub design pattern?
  3. Is it better to maintain all the children on the parent?

The idea here is, parent should publish an event, and child should subscribe those events. Parent publish the event, based on the event in child. It's kind of confusing here, because parent is listening on the child's event and child is listening on Parent's event.

I tried using pub/sub pattern, but I am not sure how to use it in the context above. Somehow, Parent should not worry about the children and publish the message. But that publish should happen on some state change in children.

Can someone point me to the right design pattern here?

Aucun commentaire:

Enregistrer un commentaire