Composer.js
- Start
- Class system
- Eventing
- Utilities
- Base
- Model
- Collection
- Controller
- Controller xdom
- Relational model
- Filter collection
- List controller
- Router
- Best practices
- Building/load order
- Tests
Eventing
As mentioned in the intro, eventing is the glue that ties everything in your app together. Actions (and associated data) flow from component to component using events. Instead of your objects needing to know the implementation details of other objects (ie how do I re-render the three views I’m in when my name changes?) objects can just register to listen to events of those they are interested in.
A controller can listen to change events in a model to determine if it should update the view. A controller can listen to events from the view (clicked link, form submitted, etc) and can update its model(s) accordingly.
Events let you wire eveything in your app together while keeping things simple and maintainable.
Built-in events
Many objects (models, collections, controllers, etc) trigger built-in events
when certain actions happen on them. For instance, a model triggers a change
event when it is updated with new data.
This allows you to listen to events for many common actions in your objects and act accordingly.
all
When any event is triggered, it also fires an “all” event. This event can be bound to in order to be notified of any events occuring on an object. The “all” event fires with the arguments being the name of the actual event being fired, and the rest of the arguments appended:
Silencing
Many (if not all) built-in events can be silenced during various operations by
passing a special object into the triggering function’s options
.
This can be done in three ways:
Composer.Event
This is the main class others that need eventing extend. It provides some simple but powerful functions.
bind :: function(event_name, fn, bind_name)
Bind a function to an event on this object. event_name
can be any string (ie
change
, click
, etc). Note that event_name
can also be an array of strings,
and each one will have the same function bound to it.
fn
is a function to call when the event is triggered on the object. It can
take any number of arguments, which are passed in verbatim from the
trigger function.
bind_name
is the name of the binding. This is mainly useful when you are
binding a function to an event that you don’t want to keep a reference to (an
anonymous function, for instance) in the case that you later want to unbind
it. Naming allows you to unbind by a unique name instead of keeping the
reference to the function.
bind_once :: function(event_name, fn, bind_name)
Exactly like bind, except that once the binding is triggered, it
unbinds itself from the object. Note that event_name
can be an array of
strings, and each one will have the same function bound to it.
This makes it easy to create one-off bindings to event on an object without having to worry about manually unbinding the event.
unbind :: function(event_name, function_or_name)
Unbind an event from an object. event_name
is the event name that’s bound and
function_or_name
is either the reference to fn
(passed into bind or
bind_once) or the bind_name
passed in.
Note that event_name
can be an array of strings, and each one will have the
same function (or binding name) unbound from it.
If function_or_name
is falsy (null, false, etc) then all bindings under
event_name
are removed.
If event_name
is falsy (null, false, etc) then all bindings in the object
are removed. All of them.
Example usage:
trigger :: function(event_name, …)
Trigger an event on an object. event_name
can be any string, and any other
arguments passed to trigger
are available as the arguments the the functions
bound with bind.
trigger
also fires an event called “all” each time it is called, the arguments
of the event being event_name
with the other arguments appended. This lets you
bind to any event triggering.