Clarification:
I’m only targeting modern browsers that support createEvent, addEventListener, etc….
Question
I mean natively, in JavaScript. A search shows that they are not using createEvent().
I’m looking here in the documentation for answers but did not find any.
I did a search for createEvent() in the source here but there were no hits.
How does backbone implement events from a native language perspective?
Are they using the observer pattern?
If JavaScript already has custom events ( via createEvent() ) available, and event listeners available as well ( addEventListener() why don’t they use the built in events?
Why isn’t Backbone using native events?
It doesn’t make sense to use native events for anything but a backbone view. Backbone provides event management for models, collections, and generally
Native events (via
createEvent,addEventListener) are tied to a DOM node. Models and collections are not associated to a DOM node – you’d have to jump through a lot of unnecessary hoops to reuse that code.Do Backbone events follow the observer pattern?
Not really, no.
Backbone.Eventsis a lot closer to the publish-subscribe pattern. An object maintains a list of event listeners for a named event (viaon), and then that object triggers a call to those listeners viatriggerwhen it wants to fire an event.How are Backbone events implemented?
At its core, Backbone maintains an array of event listeners per event (and in older versions of backbone, it used to use a linked list; that was slower). Each object maintains its own list of listeners; there is no central registry.
On
trigger, backbone calls all the listeners registered for that event; and it also calls any listeners for the special-casedallevent.