is serviceLocator really meant to work like this? #2504
Replies: 2 comments
-
I was going to use a DI pattern but that added a bunch of dependencies that I didn't like. Previously, in the constructor of the main class and the dates class I'd new up the dependent classes. I think this was causing issues creating plugins because some undefined. There was also circular dependencies and I had to pass around the "context". This pattern was my, perhaps poor, attempt to resolve that. I took some concepts from type-di to make it work without all the extra stuff. I'd be interested a PR if you have a better idea. I have some pending stuff to push so maybe give it a day. |
Beta Was this translation helpful? Give feedback.
-
the one that i come up with right now is something like this:
this way the component is the root over everything. now you can also do constructor(private serviceLocator: ServiceLocator) { in the Dates constructor and can use it at any time.. |
Beta Was this translation helpful? Give feedback.
-
ServiceLocator is kind of an singleton service.
Problem is that it is created in the constructor of the component: https://github.com/Eonasdan/tempus-dominus/blob/development/src/js/tempus-dominus.ts#L37
and at that time you have to use it to create all the objects from that service, (like the 5 lines above that L37 line above)
but also all the services like Dates must do this then in the constructor (as they do right now)
https://github.com/Eonasdan/tempus-dominus/blob/development/src/js/dates.ts#L16
because if you wouldn't do that, and you would use the serviceLocator somewhere in a method that is called because of an event trigger. this would very likely suddenly be a completely different serviceLocator object instance (that another calendar field has created)
Was this meant to work like this? Because it seems that what is tried to being done is that the TempusDominus object (the component) is a serviceLocator for all its sub objects (like Dates)
But maybe for this is it not nice to have an Interface ServiceLocator that TempusDominus implements and that the cache an creation of other obejcts are in there, and it knows that it needs to call the constructor with the ServiceLocator instance (itself). Because all the objects must have a constructor with 2 param which is a ServiceLocator?
Beta Was this translation helpful? Give feedback.
All reactions