0

Why does the default function for my event not execute when I specify it with defaultFn?

var node = Y.one(".foo");

node.publish("myEvent", {
    defaultFn: function () {
        //I don't understand why this doesn't execute
        //I expect it to execute after my on listener
        //and before my after listener
        Y.log("In default function");
    }
});

node.before("myEvent", function () {
    Y.log("In before listener");
 });
node.on("myEvent", function () {
    Y.log("In on listener");
});
node.after("myEvent", function () {
   Y.log("In after listener");
});

node.fire("myEvent");

JSFiddle: http://jsfiddle.net/steaks/rUacD/

Steven Wexler
  • 16,589
  • 8
  • 53
  • 80

1 Answers1

1

Your jsfiddle shows you are loading only node. To have events, you have to load one of the event modules. As it happens, your code fails at the call to publish, the first method that event provides that it finds.

In general, check the API docs for the methods that fail and see which module provides them ('Inherited from' right below the heading for the method) and make sure you have the corresponding module loaded.

-- Updated according to newer JSFiddle:

Add emitFacade: true to the configuration of the published event. YUI does this automatically for classes inheriting from Base but not for others. For Node instances, you have to add that option explicitly.

user32225
  • 854
  • 7
  • 7
  • I do have events. That's why I can fire and listen to events. It still doesn't work when I add `event-custom`: http://jsfiddle.net/steaks/rUacD/4/ – Steven Wexler Aug 09 '13 at 14:12
  • Now you do, in version 4, there wasn't any events in the jsfiddle you first showed. – user32225 Aug 10 '13 at 15:20
  • You'll find that version 1 is identical to version 4. I was trying to load event modules per your suggestion. Then I reverted my changes when I didn't find a solution. – Steven Wexler Aug 10 '13 at 17:29
  • In response to your edit. I had tried setting emitFacade to true. No dice. The documentation about default functions also suggest that defaultFn is not dependent on emotFacade. – Steven Wexler Aug 11 '13 at 04:04
  • It doesn't? Then perhaps I missread the first paragraph here: http://yuilibrary.com/yui/docs/event-custom/index.html#facade – user32225 Aug 11 '13 at 19:17
  • I read over the paragraph you referenced. You're absolutely correct! However, I still don't understand why http://jsfiddle.net/steaks/rUacD/6/ doesn't execute the default function. – Steven Wexler Aug 12 '13 at 14:53
  • Your answer is correct. Here is a working JS fiddle: http://jsfiddle.net/steaks/rUacD/11/ – Steven Wexler Aug 13 '13 at 15:23
  • Great! A further improvement: you can do away with the check on `domready` by placing your ` – user32225 Aug 13 '13 at 15:57