Y.Node addTarget事件没有冒泡

时间:2013-08-08 04:41:55

标签: javascript events yui event-bubbling

为什么我的活动在foo冒充bar后才被解雇?

var foo = Y.one(".foo"),
    bar = Y.one(".bar");

foo.addTarget(bar);

foo.on("myEvent", function () {
    //this log statement executes
    Y.log("In foo listener");
});

bar.on("myEvent", function () {
    //this log statement doesn't execute.
    //I don't understand why.  I think it
    //should because I expect myEvent to 
    //bubble from foo to bar since I used
    //addTarget
    Y.log("In bar listener");
});

foo.fire("myEvent");

JS小提琴:http://jsfiddle.net/steaks/tJnLf/

1 个答案:

答案 0 :(得分:2)

您必须从myEvent发布foo并将emitFacade设置为truehttp://yuilibrary.com/yui/docs/event-custom/#facade

http://jsfiddle.net/tJnLf/27/

YUI().use('event-custom', 'node', function(Y) {
    Y.on('domready',function(e) {   
        var foo = Y.one(".foo"),
            bar = Y.one(".bar");

        foo.publish("myEvent", {
            emitFacade: true
        });

        foo.addTarget(bar);

        foo.on("myEvent", function () {
            Y.log("In foo listener");
        });

        bar.on("myEvent", function () {
            Y.log("In bar listener");
        });

        foo.fire("myEvent");
    });
});