[ 3 posts ]

Nick Husher

YUI Contributor

  • Offline
  • Profile
Tags:

EventTarget's defaultTargetOnly config property?

Post Posted: Thu Oct 27, 2011 9:30 am
+0-
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?

Luke Smith

YUI Contributor

  • Username: lsmith
  • Joined: Thu Aug 28, 2008 7:50 am
  • Posts: 507
  • Location: Sunnyvale
  • Twitter: ls_n
  • GitHub: lsmith
  • Gists: lsmith
  • IRC: ls_n
  • YUI Developer
  • Offline
  • Profile

Re: EventTarget's defaultTargetOnly config property?

Post Posted: Thu Oct 27, 2011 11:03 am
+0-
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).

Nick Husher

YUI Contributor

  • Offline
  • Profile

Re: EventTarget's defaultTargetOnly config property?

Post Posted: Thu Oct 27, 2011 1:38 pm
+0-
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
  [ 3 posts ]
Display posts from previous:  Sort by  
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