lundi 6 juillet 2020

Is it OK to use a module to communicate two different JavaScript modules?

I'll structure this question in two parts: a general part and a specific one to my problem.

Generally

Is it OK to use a module to communicate two JavaScript modules?

I am not talking about events. Let's say something happens in module1 and some time after in module2 I'd like to retrieve the information generated by module1. I would create a module exporting an object and in this object module1 can write whatever it wants and module2 can read it and vice versa.

Is this approach ok? What would be a better approach? I don't want to leave the object neither in module1 nor in module2 because they are hierarchically in the same level.

Specifically

Would this pattern be OK to use in Vue.js?

I don't want to use:

  • Event bus, because there isn't one event happening. Also it seems like over complicating things.
  • Vuex. I am using the store but I don't want to mess it. I plan on using the store to save it directly to disk.

The case

I have one Vue component (component1) that starts a drag and drop event. The drag events must set an ID because component1 should only start the event but after that it should listen to other events (dragenter, dragover) but only conditionally depending on its ID. Then component2 should listen for events while dragenter or dragover event to do stuff depending on the ID set by component1. I would set the ID in an external module so both can access it.

I was planning on using either dragenter or dragover together with event.dataTransfer.getData() but these events aren't able to read the data in dataTransfer (link).

Aucun commentaire:

Enregistrer un commentaire