Search code examples
javascripteventsyui

YUI custom event default function not executing


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/


Solution

  • 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.