Event handling libraries for javascript
I'm thinking of adding some levels and achievements to a javascript based game. Since the points scoring is already there, it would be really nice if I could just have the scoring emit some kind of event that can be caught by the level and achievements code. In this way there would be a reasonably clean and simple interface between the code sections.
So I went to the Microjs site and found a dozen or so libraries that handled events. They seemed to fall into three categories:
- Libraries only interested in the DOM. I wanted something a bit more abstract, decoupled from the DOM.
- Libraries that modify the objects they are operating on. This seemed to me to not be very transparent, and I would need to come up with objects. Since it is a mobile application I need to persist stuff through the create/pause/resume/destroy lifecycle, and i do not want to end up with some objects - that have data that is persisted - also have some state related to what events they emit or listen to, while others objects may be loaded from script with no memory of this
- So, the third group uses a dedicated central object, that just emits nice clean text messages. These libraries seem to be classified as doing the pubsub pattern or mediator pattern. This I believe is what I want.
Candidates in group 3 so far are:
- Mediator.js
- Radio.js | Chainable and Depencency-free Pub/Sub for JavaScript
- federico-lox/pubsub.js
-
pazguille/jvent
A light utility class to help implement the Mediator pattern
Read more: Link - Mediator.js