Luke Smith![]()
This patch adds missing logic in YUI 3.3.0's event system whereby preventing an event from a bubble target did not trigger the event's preventedFn.
See the example snippet below for the case where the behavior fails without this patch.
This patch module is only necessary for version 3.3.0.
Apply the patch in the use() statement as long as version 3.3.0 is in use. Remove from the use() statement after upgrading to the subsequent version.
<script src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js"></script>YUI({
//Last Gallery Build of this module
gallery: 'gallery-2011.03.09-21-14'
}).use('event-custom', 'gallery-patch-330-event-preventedfn', function(Y) {
// Create two EventTargets (typically, these will be class instances that inherit from EventTarget
var a = new Y.EventTarget({ emitFacade: true, prefix: 'a' }),
b = new Y.EventTarget({ emitFacade: true, prefix: 'b', bubbles: true });
// Events from b will bubble to a, allowing a.on('b:foo', fn)
b.addTarget(a);
// The child publishes an event with a preventedFn, so when e.preventDefault() is called from a
// subscriber, this function should execute instead of the defaultFn
b.publish('foo', {
defaultFn: function () { Y.log('default'); },
preventedFn: function () { Y.log('prevented'); }
});
// Set up event subscribers to every phase of the event lifecycle
b.on('foo', function () { Y.log('b on foo'); });
b.after('foo', function () { Y.log('b after foo'); });
a.on('b:foo', function () { Y.log('a on foo'); });
a.after('b:foo', function () { Y.log('a after foo'); });
// If the event is not prevented, the following messages should be logged in order:
// b on foo
// a on foo
// default
// a after foo
// b after foo
// Preventing the event from a bubble target subscriber should trigger the preventedFn.
// This is not happening in 3.3.0 without this patch
a.on('b:foo', function (e) { e.preventDefault(); });
b.fire('foo');
// output before (notice no defaultFn, but also no preventedFn):
// b on foo
// a on foo
// output with patch
// b on foo
// a on foo
// prevented
});
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info