I am using aurelia-store to give me important updates on changes on choosenCompanyId since this have important filtering implications.
but if i only listen to choosenCompanyId in example my dropdownlist custom-element and my singleton Data Class. i will get random timing issues due to both parts listening to the same command.
I Solved this by making the data class dispatch another action that the element is listening to. but i dont like this. since i already have to inject the class regardless. it feels redudant.
I have to admit that I absolutely dont get what youâre after the example is also way too complex with parts that arent related to the question like this.promise but adding more clutter. Could you try to build a minimalistic sample which you can share either via GitHub or codesandbox. Especially also try to point out exactly what that timing issue is that youâve mentioned.
There are multiple things to note here but without the full idea what youâre exactly after its hard to propose anything.
The return of this.promise is for other classes or elements in the future to receive the data without having to do a new api call.
And in this example there is no timing issues since i am updating the data class first then dispatching an update to the store which the element is listening to.
my question is more in terms of. ok i am listening to the store now. but i still have to inject the data class into the element to make shure the dataclass is somehow booted up somewhere, it dosnt need to be this element. but say i have 10 different elements that need the same data . but u dont know which one needs it first. i dont want to initialise everything at the start of the application. i want on a need basis. but when it its loaded it is cached until somone changes the company Id.
so the project used a dataclas with a simple promise to cache the data. then i realized i need to relead the cached data whenevern the companyId changes. hence thats why the dataclass is listening to choosenCompanyId. Still not a problem to just inject the dataclass wherever i want it.
then comes my @customElement(âtransaction-categories-lookupâ) that i am doing some postprocessing on the data. I kinda do realise in this moment when writing this. that i could probably get by just doing that filtering alsp in the categoryOptionsFunction which is what returns data to the lookup field. But lets entertain the ide that i want the this.filterTransactionCategoriesByType(); to be run whenever the data in the dataclass has changed. 1 solution is what i already done in the example. the dataclass dispatches a new event that the element is listening to.
Buuuut. this just feels a but redundant. i am both injecting the dataclass and listening to the store instead of getting the valyes directly just so that i can handle the timing. is there a smoother way of doing that. and keeping the dependency injection intact. one of the good benefits of DI is that you get stuff you need when u need it.
If I understand correctly, this is actually a fairly common design problem in SPAs and one that does not have a single (or easy) answer. It could be worth expanding upon with examples, but letâs distill the problem down to a few bullet points first:
There is server-side data which is needed by multiple components
This data is retrieved based on some ID selection which can be changed by user, but also may have some default value on initial navigation.
This data should only be loaded when needed by one or more components
The key moving part here is a singleton service / data store that aforementioned components (directly or indirectly) retrieve the data from. If multiple components request the data at the same time, only one request goes out and the others will await a cached promise.
To unify the logic for initial load with the logic for selection change, I tend to use the router. The selected id would be a parameter, and you would load the data in the (async) activate hook. This would also block rendering of child components that need the data and thus kind of solves multiple problems at once.
It doesnât seem very intuitive to me to try to have a dropdown selection change going through the store and letting the store handle all of this infrastructure and application-level logic, but I donât know the store that well. @zewa666 is there perhaps some neat way to pipe events into async operations or would you agree that this might be a job better suited for the router or some other non-store mechanism (like event aggregator)?
I think you are kinda spot on in your way of describing the problem. having that singleton service class makes life easy for the most part. but when i need it updated based on some changes in parts where i dont controll or atleast not part of the current viewmodel.
but at the moment the dropdown that chooses company resides outside the aurelia app. so i am just listening to a event from that dropdown. and then dispatching a store action from that. this way there is a very small interaction between the âold legacyâ code and the new aurelia app. and when or of at all i will get time to move the dropdown and all that stuff into aurelia. it will be very easy to dispatch the same action. from the new element. not needing to change anything else.
and to clarify the store aproach is working for the most part splended. The real issue as i mentioned is how to handle the timings if not using the store.
example 1. component/elment A need to listen to companychange and update still not a problem you just connect the component/elment to the store and listen to changes on company. Good stuff no problem or ugly code.
example2. Comonent/Element B need to run some logic/update when Component/element A has updated becouse of some other event example like that company changed.
if i am using store i have to connect component/element B to the store. nice stuff no problems here.
But now comes that part that makes compining these two principle stupid.
To make shure that my singleton is booted up at some time. i have to inject component A into component B even if the data flow comes throught the store.
And maybe using store here is not the best way to do it at all. but what would be the better option to solve it. i dont think code example is needed here becouse this is more general principles then code implementaion.