Adds publish/subscribe channels. Unit tests added.

This commit is contained in:
jaubourg 2011-05-24 01:59:00 +02:00
parent 9edc3d4f39
commit 96b0089c9c
4 changed files with 93 additions and 0 deletions

34
src/channel.js Normal file
View file

@ -0,0 +1,34 @@
(function( jQuery ) {
var channels = {},
channelMethods = {
publish: "fire",
subscribe: "add",
unsubscribe: "remove"
};
jQuery.Channel = function( name ) {
var callbacks,
method,
channel = name && channels[ name ];
if ( !channel ) {
callbacks = jQuery.Callbacks();
channel = {};
for ( method in channelMethods ) {
channel[ method ] = callbacks[ channelMethods[ method ] ];
}
if ( name ) {
channels[ name ] = channel;
}
}
return channel;
};
jQuery.each( channelMethods, function( method ) {
jQuery[ method ] = function( name ) {
var channel = jQuery.Channel( name );
channel[ method ].apply( channel, Array.prototype.slice.call( arguments, 1 ) );
};
});
})( jQuery );