Created
September 14, 2012 08:27
-
-
Save ambar/3720749 to your computer and use it in GitHub Desktop.
jQuery 1.8 + data events intercept
This file contains hidden or 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 lang="en-US"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
<style type="text/css"> | |
button { | |
display: block; | |
margin: 5px; | |
} | |
</style> | |
</head> | |
<body> | |
<button id=":1">button</button> | |
<button id=":2">button</button> | |
<button id=":3">button</button> | |
<button id=":4">button</button> | |
<!-- | |
http://blog.jquery.com/2012/06/22/jquery-1-8-beta-1-see-whats-coming-and-going/ | |
#10589: Remove $.fn.data("events") | |
--> | |
<script src="http:////cdnjs.cloudflare.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> | |
<!-- <script src="http:////cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> --> | |
<script> | |
INTERCEPTED = '__intercepted_hidden_prop__' | |
var intercept = function(fn, msg) { | |
if (fn[INTERCEPTED]) return fn | |
var intercepted = function() { | |
if (confirm(msg)) { | |
fn.apply(this, arguments) | |
} | |
} | |
intercepted[INTERCEPTED] = true | |
return intercepted | |
} | |
$buttons = $('button') | |
$buttons.on('click.intercept', function(e) { | |
$buttons.each(function() { | |
// var evts = $.data(this, 'events') | |
// jQuery 1.8 + | |
var evts = $._data(this, 'events') | |
evts.click | |
.filter(function(e){ | |
return e.namespace !== 'intercept' | |
}) | |
.forEach(function(e) { | |
e.handler = intercept(e.handler, 'wtf') | |
}) | |
}) | |
}) | |
$buttons.on('click',function(e) { | |
console.info(this.id, this, e) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment