Archived Repository This code is no longer maintained. Feel free to fork it, but use it at your own risks. |
Brings channels to AngularJS.
It is available with bower:
bower install ng-channel
Then add the retrieved files to your HTML layout:
<script type="text/javascript" src="/path/to/bower_components/ng-channel/ng-channel.min.js"></script>
You can also use it with RequireJS as an AMD module.
Then add ngChannel
as dependency for your AngularJS application:
var app = angular.module('YOUR_APP', ['ngChannel']);
Out of the box, ngChannel
uses $$asyncCallback
from angular to call asynchronously the channels' subscribers. You can provide an another by calling $channelFactory.asyncCallbackFactory(YOUR_FACTORY)
.
ngChannel exposes a service $channelFactory
to build channels:
var channel = $channelFactory();
You can now pipe
as many subscribers as you wish to your channel:
channel.pipe(function(message) {
// message is the one broadcasted in the channel
});
// if you wish you can use dependency injection
channel.pipe(['myService', function(myService) {
return function(message) {
// This is a subscriber but you can now deal with myService
};
}]);
To send a message in the channel, call send
method:
channel.send('Yeah Ho!');
You can also pipe a channel with others channels:
var otherChannel = $channelFactory();
channel.pipe(otherChannel);
otherChannel.pipe(function(message) {
// this subcriber will also be triggered by any message send in `channel`
});
Furthermore you can pipe the output of a subscriber:
channel.pipe(function(message) {
return 'Ho!'
}).pipe(function(message) {
// message is now `Ho!`
});
// If you wish you can also pipe it to another channel
channel.pipe(function(message) {
return 'Ho!'
}).pipe(anotherChannel);
If you just want to add several subscribers at once on the same channel, you can use pipeChain
:
channel
.pipeChain(function(message) {
// it will receive message from channel
})
.pipeChain(function(message) {
// it will receive message from channel and not from the previous subscriber
});
To rebuild the minified JavaScript you must run: make build
.
Install dependencies and run the unit tests:
make install
make test-spec
All contributions are welcome and must pass the tests. If you add a new feature, please write tests for it.
This application is available under the MIT License, courtesy of marmelab.