Created
May 19, 2010 00:25
-
-
Save rwaldron/405760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>$.detonate()</title> | |
<script src="http://code.jquery.com/jquery.js"></script> | |
<script src="jquery.detonate.js"></script> | |
<script> | |
$(function () { | |
$.charge( | |
'/fuse', | |
function(msg) { | |
console.group('/fuse'); | |
console.log(msg); | |
console.groupEnd(); | |
} | |
); | |
$.charge( | |
'/fuse/a', | |
function(msg) { | |
console.group('/fuse/a'); | |
console.log(msg); | |
console.groupEnd(); | |
} | |
); | |
$.charge( | |
'/fuse/b', | |
function(msg) { | |
console.group('/fuse/b'); | |
console.log(msg); | |
console.groupEnd(); | |
} | |
); | |
$.charge( | |
'/fuse/c', | |
function(msg) { | |
console.group('/fuse/c'); | |
console.log(msg); | |
console.groupEnd(); | |
} | |
); | |
/***********************/ | |
$.detonate('/fuse/c', ['message']); | |
$.detonate([ | |
{ | |
fuse: '/fuse/a', | |
args: ['message from fuse a'] | |
}, | |
{ | |
fuse: '/fuse/b', | |
args: ['message from fuse b'] | |
}, | |
{ | |
fuse: '/fuse/c', | |
args: ['message from fuse c'] | |
} | |
]); | |
$.detonate([ | |
{ | |
fuse: '/fuse/a' | |
}, | |
{ | |
fuse: '/fuse/b' | |
}, | |
{ | |
fuse: '/fuse/c' | |
} | |
]); | |
}); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
jQuery.detonate() | |
jQuery.charge() | |
2010 Rick Waldron | |
*/ | |
;(function($){ | |
function disarm(fuse){ | |
var t = fuse[0]; | |
$.each(_payloadCache[t], function(index){ | |
if(this == fuse[1]){ | |
_payloadCache[t].splice(index, 1); | |
} | |
}); | |
} | |
function blast(fuses, args){ | |
$.each(_payloadCache[fuses], function(){ | |
this.apply($, args || []); | |
}); | |
} | |
var _payloadCache = {}; | |
$.extend(jQuery, { | |
detonate: function(fuses){ | |
if ( typeof fuses === 'string' ) { | |
var args = arguments[1] ? arguments[1] : []; | |
blast(fuses, args); | |
return; | |
} | |
$.each(fuses, function (i, fuse) { | |
if ( fuse.relay ) { | |
// NOT IMPLEMENTED. | |
} | |
blast(fuse.fuse, fuse.args || [] ); | |
}); | |
}, | |
charge: function(fuse, callback) { | |
if ( fuse === 'disarm' ) { | |
fuse = callback; | |
disarm(fuse); | |
return true; | |
} | |
if(!_payloadCache[fuse]){ | |
_payloadCache[fuse] = []; | |
} | |
_payloadCache[fuse].push(callback); | |
return [fuse, callback]; // Array | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment