Are there any patterns that illustrate how to avoid magic strings for event names when using loosely-coupled Javascript modules (in a web browser) that make use of a Pub/Sub library?
I'm using require.js and amplify.js to develop a modular-based web application. I'm using amplify to publish and subscribe to events, but of course this requires that both the publisher and subscriber know what the event label is. I'd like to avoid using magic strings for these event labels. I was thinking that each module would have an associated "eventsList.js" file, or something similar that would list the events that module would publish. For any module that wants to subscribe to the events of other modules, it would import every event list for every module of interest, which would also prevent the event labels from leaking into the global namespace.
I specifically want to AVOID having a "master event list" for every module in my application.
Has anyone achieved something similar?
I'm also concerned about using magic strings for my topics. Did you achieve a suitable solution? I've currently gone for the master event list which i'm not particularly happy with:
var app = {
events: {
mod1: {
topica: 'topic-a',
topicb: 'topic-b'
},
mod2: {
topicx: 'topic-x',
topicy: 'topic-y'
}
}
}
//usage
amplify.publish(app.events.mod1.topica)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With