Adds publish/subscribe channels. Unit tests added.
This commit is contained in:
parent
9edc3d4f39
commit
96b0089c9c
4 changed files with 93 additions and 0 deletions
34
src/channel.js
Normal file
34
src/channel.js
Normal 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 );
|
Loading…
Add table
Add a link
Reference in a new issue