Skip to content

Instantly share code, notes, and snippets.

@lsmith
Created February 17, 2012 20:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmith/1855345 to your computer and use it in GitHub Desktop.
Save lsmith/1855345 to your computer and use it in GitHub Desktop.
Proof of concept support for multiple event behaviors
YUI.add('event-multi-defaultFn', function (Y) {
// FIXME: publish() needs to be patched to support allowing this to be called before
// publish(). Today, the publish() call will clobber the AOP wrapper.
Y.EventTarget.prototype.addEventBehavior = function (type, fn, when) {
var event = this.getEvent(type),
method = when === 'before' ? 'before' : 'after',
handle;
if (event && event.defaultFn) {
handle = Y.Do[method](fn, event, 'defaultFn');
}
return handle;
};
// detach from the returned handle also works, and calls to both
// handle.detach() and thing.removeEventBehavior(...) should not
// interfere with the other.
Y.EventTarget.prototype.removeEventBehavior = function (type, fn, when) {
var event = this.getEvent(type),
eventId, doMethod, id;
if (event && event.defaultFn && fn) {
eventId = Y.stamp(event);
doMethod = Y.Do.objs[eventId];
id = eventId + Y.stamp(fn) + 'defaultFn';
if (doMethod) {
if (when) {
when = when === 'before' ? when : 'after';
delete doMethod.defaultFn[when][id];
} else {
delete doMethod.defaultFn.before[id];
delete doMethod.defaultFn.after[id];
}
}
}
};
}, '0.1', { requires: ['event-custom'] });
<html lang="en">
<head>
<title>POC event behavior phase extension</title>
<meta charset="utf-8">
<style>
body { font-family: sans-serif; }
</style>
</head>
<body>
<h3>Firing with on(), native defaultFn, after()</h3>
<ol id="one"></ol>
<h3>Firing with added behavior after native defaultFn</h3>
<ol id="two"></ol>
<h3>Firing behaviors added before and after native defaultFn</h3>
<ol id="three"></ol>
<h3>Firing again after adding and removing more behaviors</h3>
<ol id="four"></ol>
<script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script>
<script src="event-multi-defaultFn.js"></script>
<script>
YUI({ filter: 'raw' }).use('node', 'event-multi-defaultFn', function (Y) {
var target = new Y.EventTarget({ emitFacade: true }),
current = 'one';
function report(message) {
Y.one('#' + current).append('<li>' + message + '</li>');
}
target.publish('foo', {
defaultFn: function () {
report('published defaultFn');
}
});
target.on('foo', function () {
report('on subscriber');
});
target.after('foo', function () {
report('after subscriber');
});
target.fire('foo');
current = 'two';
target.addEventBehavior('foo', function () {
report('after published defaultFn');
}, 'after');
target.fire('foo');
current = 'three';
target.addEventBehavior('foo', function () {
report('before published defaultFn');
}, 'before');
target.fire('foo');
current = 'four';
var handle = target.addEventBehavior('foo', function () {
report('this should not be logged');
}, 'before');
handle.detach();
function removeMe() {
report('this should ALSO not be logged');
}
target.addEventBehavior('foo', removeMe, 'after');
target.removeEventBehavior('foo', removeMe, 'after');
target.fire('foo');
});​
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment