| Page 1 of 1 | [ 3 posts ] |
|
When defining a new custom event on an EventTarget, there's an undocumented configuration property defaultTargetOnly. A little digging seems to indicate that it has something to do with how defaultFns are called.
What's the purpose of this configuration attribute? |
|
It's kind of a mind bender, but IMO should be the default behavior.
If an event is published with defaultTargetOnly and bubbles to a target that also has an event published with the same name and its own defaultFn, only the defaultFn of the instance that originated the event will be executed. So let's say Code: bee.publish('buzz', { defaultFn: bee._defBeeBuzzFn }); hive.publish('buzz', { defaultFn: hive._defHiveBuzzFn }); bee.addTarget(hive); bee.fire('buzz'); Because event bubbling is translated into firing the event from each bubble target, hive's _defHiveBuzzFn would execute in addition to bee._defBeeBuzzFn because bee.fire('buzz') resulted in hive.fire('buzz'). By adding Code: defaultTargetOnly: true , only bee._defBeeBuzzFn would execute.The example is contrived, but it can be an issue when class instances can nest within other instances of the same class (think a generic node class for creating a tree structure). |
|
A perfect explanation, thank you Luke!
I can see this being a problem if you had a tree data structure: Code: var TreeNode = Y.Base.create('treenode', Y.Base, [], { /* ... */ }, { /* ... */ }); var root = new TreeNode(), leaf = new TreeNode(); root.add(leaf); leaf.fire('someEventWithADefaultFn'); // would run the defaultFn on both root and leaf |
| Page 1 of 1 | [ 3 posts ] |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 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
Powered by phpBB® Forum Software © phpBB Group