私が書いたクラスから必死にイベントを発生させようとしています。YUI ウィジェット内でこのクラスを使用し、発生したイベントに YUI ウィジェットが応答するようにします。イベントのバブリングがどのように機能しているかはわかっているので、このコードは問題なく機能しています。
YUI().use('event-custom', 'node', function (Y) {
function Publisher(bubbleTo) {
this.addTarget(bubbleTo);
this.publish("testEvent", {
emitFacade: true
});
this.fire("testEvent");
}
function BubbleTarget() {
this.on("testEvent", function (e) {Y.log("Bubbling in Test.js succeed!")});
var newPublisher = new Publisher(this);
}
// To fire events or be a bubble target, augment a class with EventTarget
Y.augment(Publisher, Y.EventTarget);
Y.augment(BubbleTarget, Y.EventTarget);
var bubbleTarget = new BubbleTarget();});
ただし、この概念をウィジェットに適用しようとすると、非常に失敗します。
YUI.add("SlideShow", function(Y) {
function SlideShow(config) {
SlideShow.superclass.constructor.apply(this, arguments);
}
SlideShow.NAME = "SlideShow";
Y.extend(SlideShow, Y.Widget, {
initializer: function() {
Y.log("Widget loaded!");
this.on("testEvent", function () {
Y.log("This should, but won't appear despite how hard I try!");
});
},
renderUI: function(){
var testSlide = new Slide("text", this);
}
});
Y.SlideShow = SlideShow;
function Slide(sendTo)
{
this.addTarget(sendTo);
this.publish("testEvent", {
defaultFn: function(){Y.log('Event published.')},
emitFacade: true
});
this.fire("testEvent");
}
Y.augment(Slide,Y.EventTarget, true, null, {emitFacade: true});
}, "0.0.1", {requires:["widget","event-custom","node","anim"]});
Chromium のログ出力は次のとおりです。
- Test.js でのバブリングが成功しました!
- ウィジェットがロードされました!
- イベント公開。
Widgets に不可欠な何かが本当に欠けているようです。この問題をよりよく理解するために、私を助けてください。