EventBus Module Example
In step three of the AngularJS demo (http://docs.angularjs.org/tutorial/step_03) I didn't like how coupled the "widgets" were so I set out trying to figure out what support there was for an event bus in Angular. It appears to be easy enough by leveraging $rootScope.$broadcast. I found this example of an event bus module here (http://jsfiddle.net/simpulton/GeAAB) so I took that and simplified it plus added support for event tracing (which is great for event discovery). My jsFiddle is linked here (http://jsfiddle.net/Aberwyvern/7tA4v/9/) and includes the rewrite of Step 03 of the AngularJS demo to be modular and operate through a common message bus instead of being directly tightly coupled. I thought other people may find this useful for their own projects so I figured I'd post it here.
by Aberwyvern
HTML
<!-- Query Input Module View START -->
<div ng-controller="QueryInputController">
Search: <input ng-model="queryInput" ng-change="queryUpdated()" />
</div>
<!-- Query Input Module View END -->
<br>
<!-- Search Results Module View START -->
<span ng-controller="SearchResultsController">
<ul>
<li ng-repeat="phone in phones | filter:activeFilter">
{{phone.name}}
<p>{{phone.snippet}}</p>
</li>
</ul>
</span>
<!-- Search Results Module View END -->
JavaScript
'use strict';
//EventBus MODULE START
//Defined in EventBusModule.js
angular.module('EventBusModule', []).factory('EventBus', function($rootScope) {
window.__EVENT_BUS_CAPTURE__ = false;
return {
//Arguments to this method are
//0: custom event name
//...: other arguments to the event
broadcast : function() {
//arguments isn't actually an array, but $broadcast expects it be...
var broadcastArgs = Array.prototype.slice.call(arguments);
//If the __EVENT_BUS_CAPTURE__ global is set to the emit events
//passing through the message bus. This can be undefined by
//default and turned on from the console if we want to see
//them to help with event discovery.
if (window.__EVENT_BUS_CAPTURE__ && window.console)
{
console.log({
TraceSource : "EventBus.Broadcast (" + arguments[0] + ")",
Arguments : broadcastArgs
});
}
//Broadcast through the root scope so the event is
//broadcast through all other scopes.
$rootScope.$broadcast.apply($rootScope, broadcastArgs);
}
};
});
//EventBus END
//QueryInputModule START
//Defined in QueryInputModule.js
angular.module(
"MyApp.QueryInputModule", ["EventBusModule"]
).controller(
"QueryInputController",
function($scope, EventBus) {
$scope.queryInput = "";
//When the input is updated then broadcast an event
//Other modules can now respond to this even in a decoupled fashion.
$scope.queryUpdated = function() {
EventBus.broadcast("SearchQueryChanged", this.queryInput);
};
}
);
//QueryInputModule END
//SearchResultsModule START
//Defined in SearchResultsModule.js
angular.module(
"MyApp.SearchResultsModule", ["EventBusModule"]
).controller(
"SearchResultsController",
function($scope, EventBus) {
$scope.phones = [
{
name : "Nexus S",
snippet : "Fast just got faster with Nexus S." },
{...