mjwwit / EventBroker

Global JavaScript event broker.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Brokr.io

AngularJS Event Broker. Event listeners can be processed asynchronously.

Local event broker AngularJS module uses Angular's $q service.

Global event broker uses kriskowal's q. It registers on window so it can be accessed from other AngularJS apps on the same page.

Local usage

angular.module('some.module', ['brokr'])
    .service('ModuleBroker', ['EventBrokerFactory', function (EventBroker) {
        return new EventBroker('some.module');
    }])
    .controller('SomeCtrl', ['ModuleBroker', function (ModuleBroker) {
        ModuleBroker.on('some.module', 'SOME_EVENT', alert);
        ModuleBroker.emit('SOME_EVENT', 'Hello, World!');
        
        ModuleBroker.emitAsync('SOME_EVENT', 'Hello, Async!').then(function () {
            console.log('This should log last');
        });
        console.log('This should log first');
    }]);

Global usage

angular.module('some.module', ['brokr'])
    .config(['EventBrokerFactoryProvider', function (EventBrokerFactoryProvider) {
        EventBrokerFactoryProvider.useGlobalBroker(true);
    }])
    .service('ModuleBroker', ['EventBrokerFactory', function (EventBroker) {
        return new EventBroker('some.module');
    }])
    .controller('SomeCtrl', ['ModuleBroker', function (ModuleBroker) {
        ModuleBroker.on('some.module', 'SOME_EVENT', alert);
        ModuleBroker.emit('SOME_EVENT', 'Hello, World!');
        
        ModuleBroker.emitAsync('SOME_EVENT', 'Hello, Async!').then(function () {
            console.log('This should log last');
        });
        console.log('This should log first');
    }]);

For a more advanced global example, check the Angular based demo in index.html and apps.js

About

Global JavaScript event broker.


Languages

Language:JavaScript 91.3%Language:HTML 8.7%