4👍
Object literals can store Object / classes / functions.. etc.
Your currently just storing a string, 'ReportEventService'
, so when you call the method your doing 'ReportEventService'[method]()
and that doesn’t make much sense.
But if you store the Object that is ReportEventService instead, you will be calling.. ReportEventService[method]()
IOW: service: ReportEventService
,
instead of service: 'ReportEventService'
,
2👍
If you can use any as part of eventOperations
, then there is no reason at all to use strings.
Instead make them into callbacks:
eventType: {
data: [],
method: 'getEventTypeList',
service: 'ReportEventService',
},
can become
eventType: {
data: [],
callback: () => ReportEventService.getEventTypeList(),
},
Then you can call it as
const response = await callback();
It you can use a lot more tools to verify your code. At the very least a rename refactoring will work without needing to consider random strings. You can also verify if the method exists or if it is called with the correct number of parameters.
Moreover, you also get more freedom – if you change ReportEventService.getEventTypeList()
in the future to require parameters, you can change the callback to () => ReportEventService.getEventTypeList("foo", 42)
without having to change the code that consumes it.