Created
June 16, 2010 21:19
-
-
Save rwaldron/441290 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
/* | |
jQuery.Mines.Charge/Detonate/Disarm() | |
*/ | |
;(function($){ | |
var _payloadCache = {}; | |
function blast(fuses, args) { | |
$.each(_payloadCache[fuses], function(){ | |
this.apply($, args || []); | |
}); | |
} | |
$.extend(jQuery, { | |
mines: { | |
detonate: function(fuse){ | |
if ( typeof fuse === 'string' ) { | |
var args = arguments[1] ? arguments[1] : []; | |
blast(fuse, args); | |
return; | |
} | |
$.each(fuse, function (i, _fuse) { | |
$.each(_payloadCache[_fuse.key], function(){ | |
this.apply($, _fuse.args || []); | |
}); | |
}); | |
}, | |
charge: function(fuse, callback){ | |
if(!_payloadCache[fuse]){ | |
_payloadCache[fuse] = []; | |
} | |
_payloadCache[fuse].push(callback); | |
return [fuse, callback]; | |
}, | |
disarm: function(fuse){ | |
delete _payloadCache[fuse]; | |
} | |
} | |
}); | |
})(jQuery); |
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
$(function () { | |
$.mines.charge( | |
'/fuse', | |
function() { | |
console.group('/fuse'); | |
console.log(arguments); | |
console.groupEnd(); | |
} | |
); | |
$.mines.charge( | |
'/fuse/a', | |
function() { | |
console.group('/fuse/a'); | |
console.log(arguments); | |
console.groupEnd(); | |
} | |
); | |
$.mines.charge( | |
'/fuse/b', | |
function() { | |
console.group('/fuse/b'); | |
console.log(arguments); | |
console.groupEnd(); | |
} | |
); | |
$.mines.charge( | |
'/fuse/c', | |
function(message, bool) { | |
console.group('/fuse/c'); | |
console.log(message); | |
console.log(bool); | |
console.groupEnd(); | |
} | |
); | |
/***********************/ | |
$.mines.detonate('/fuse/c', ['message']); | |
$.mines.detonate([ | |
{ | |
key: '/fuse/a', | |
args: ['message from fuse a', { foo: 'bar' }] | |
}, | |
{ | |
key: '/fuse/b', | |
args: ['message from fuse b', ['an', 'array'] ] | |
}, | |
{ | |
key: '/fuse/c', | |
args: ['message from fuse c', true] | |
} | |
]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment