gpt4 book ai didi

javascript - CLASS 中的自定义事件

转载 作者:数据小太阳 更新时间:2023-10-29 05:08:09 26 4
gpt4 key购买 nike

我需要从 CLASS 启动自定义事件。我知道用 DOM 对象和 jquery 做这个,使用 triggerHandler,比如 $(object)..triggerHandler("inputChange", {param:X});问题是当我用一个类尝试这个时,像这样:

    var MyClass = (function(){

var static_var = 1;

var MyClass = function () {

var privateVar;
var privateFn = function(){ alert('Im private!'); };

this.someProperty = 5;
this.someFunction = function () {
alert('Im public!');
};
this.say = function() {
alert('Num ' + this.someProperty);
$(this).triggerHandler("eventCustom");
}
this.alter = function() {
this.someProperty ++;
}
};

return MyClass;

})();

TheClass = new MyClass();

$(TheClass).on('eventCustom', function() {
alert('Event!');
});

TheClass.say();

这不会启动警告或错误,但事件监听器不工作(或事件未分派(dispatch))。我认为 jQuery 事件系统不适用于非 DOM 对象,对吗?

任何其他方式(我需要事件,而不是针对我的特定情况的回调)来启动事件?

非常感谢!

最佳答案

我在不使用 JQuery 的情况下用不到 100 行代码编写了一个 ES6 事件类。如果你不想使用 DOM 事件,你可以扩展你的类,它应该处理事件。

要监听事件,你可以使用on, once, onReady, onceReady。 On 是在每次触发标签时执行回调函数。一次只有一次。如果标签之前已被触发,则“就绪”函数执行回调。

要触发事件,请使用触发器。要删除事件处理程序,请使用 off。

我希望这个例子能说明问题:

class ClassEventsES6 {
constructor() {
this.listeners = new Map();
this.onceListeners = new Map();
this.triggerdLabels = new Map();
}

// help-function for onReady and onceReady
// the callbackfunction will execute,
// if the label has already been triggerd with the last called parameters
_fCheckPast(label, callback) {
if (this.triggerdLabels.has(label)) {
callback(this.triggerdLabels.get(label));
return true;
} else {
return false;
}
}

// execute the callback everytime the label is trigger
on(label, callback, checkPast = false) {
this.listeners.has(label) || this.listeners.set(label, []);
this.listeners.get(label).push(callback);
if (checkPast)
this._fCheckPast(label, callback);
}

// execute the callback everytime the label is trigger
// check if the label had been already called
// and if so excute the callback immediately
onReady(label, callback) {
this.on(label, callback, true);
}

// execute the callback onetime the label is trigger
once(label, callback, checkPast = false) {
this.onceListeners.has(label) || this.onceListeners.set(label, []);
if (!(checkPast && this._fCheckPast(label, callback))) {
// label wurde nocht nicht aufgerufen und
// der callback in _fCheckPast nicht ausgeführt
this.onceListeners.get(label).push(callback);
}
}
// execute the callback onetime the label is trigger
// or execute the callback if the label had been called already
onceReady(label, callback) {
this.once(label, callback, true);
}

// remove the callback for a label
off(label, callback = true) {
if (callback === true) {
// remove listeners for all callbackfunctions
this.listeners.delete(label);
this.onceListeners.delete(label);
} else {
// remove listeners only with match callbackfunctions
let _off = (inListener) => {
let listeners = inListener.get(label);
if (listeners) {
inListener.set(label, listeners.filter((value) => !(value === callback)));
}
};
_off(this.listeners);
_off(this.onceListeners);
}
}

// trigger the event with the label
trigger(label, ...args) {
let res = false;
this.triggerdLabels.set(label, ...args); // save all triggerd labels for onready and onceready
let _trigger = (inListener, label, ...args) => {
let listeners = inListener.get(label);
if (listeners && listeners.length) {
listeners.forEach((listener) => {
listener(...args);
});
res = true;
}
};
_trigger(this.onceListeners, label, ...args);
_trigger(this.listeners, label, ...args);
this.onceListeners.delete(label); // callback for once executed, so delete it.
return res;
}
}

// +++ here starts the example +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class TestClassEvents extends ClassEventsES6 {
constructor() {
super();
this.once('sayHallo', this.fStartToTalk);
this.on('sayHallo', this.fSayHallo);
}

fStartToTalk() {
console.log('I start to talk... ');
}

fSayHallo(name = 'Nobody') {
console.log('Hallo ' + name);
}
}

let testClassEvents = new TestClassEvents();

testClassEvents.trigger('sayHallo', 'Tony');
testClassEvents.trigger('sayHallo', 'Tim');

testClassEvents.onReady('sayHallo', e => console.log('I already said hello to ' + e));
testClassEvents.trigger('sayHallo', 'Angie');
testClassEvents.off('sayHallo');
testClassEvents.trigger('sayHallo', 'Peter');
console.log('I dont say hallo to Peter, because the event is off!')

关于javascript - CLASS 中的自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17165096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com