简体   繁体   English

NodeJS中的自定义阻止默认值

[英]Custom prevent default in NodeJS

In JavaScript, when handling an event you can call event.preventDefault() which will prevent the default action of the event.在 JavaScript 中,处理事件时可以调用 event.preventDefault() 来阻止事件的默认操作。 In NodeJS this sets event.defaultPrevented to true.在 NodeJS 中,这会将 event.defaultPrevented 设置为 true。 I want to be able to create custom emitters in my class that will default to executing something such as closing the app provided event.preventDefault() isn't called.我希望能够在我的 class 中创建自定义发射器,这将默认执行诸如关闭应用程序提供的事件。preventDefault() 不会被调用。 The issue is, there doesn't seem to be a way for the emitter to check if preventDefault() has been called as defaultPrevented is a property of the event not the emitter and as such can't be read by the emitter.问题是,发射器似乎没有办法检查 preventDefault() 是否已被调用,因为 defaultPrevented 是事件的属性,而不是发射器,因此发射器无法读取。

Here is some example code这是一些示例代码

class MyClass extends EventEmitter{
    close(winID:number):void{
        this.emmit('will-quit');
        app.close(winID);
    }
}

I want to prevent app.close() from being called if a listener for will-quit calls preventDefault.如果 will-quit 的侦听器调用 preventDefault,我想阻止调用 app.close()。

I had an idea for how to check if preventDefault has been called but it is very clunky and probably not the best idea.我对如何检查是否调用了 preventDefault 有一个想法,但它非常笨拙,可能不是最好的主意。

class MyClass extends EventEmitter{
    close(winID:number):void{
        this.once('will-quit', (event) => {
            if (!event.defaultPrevented){
                app.close(winID);
            };
        };
        this.emit('will-quit');
    }
}

With this a listener is created just before the event fires and as such it should be the last to be called.有了这个,一个监听器就在事件触发之前创建,因此它应该是最后一个被调用的。 When it is called it checks if the preventDefault has been called and if it hasn't calls the desired code.当它被调用时,它会检查是否调用了 preventDefault 以及是否没有调用所需的代码。 Because it uses once it is destroyed as soon as it is called.因为它使用一次,一调用就销毁。

I am thinking that there should be a better way of doing this but I can't really find anything in any docs or tutorials so I am wondering if this is even possible.我在想应该有更好的方法来做到这一点,但我在任何文档或教程中都找不到任何东西,所以我想知道这是否可能。 But then on that same thought frameworks like Electron seem to achieve this by using C++ modules.但是在同样的思想框架上,像 Electron 似乎通过使用 C++ 模块来实现这一点。

Note: All code samples are written in TypeScript注:所有代码示例均以 TypeScript 编写

The issue is, there doesn't seem to be a way for the emitter to check if preventDefault() has been called as defaultPrevented is a property of the event not the emitter and as such can't be read by the emitter.问题是,发射器似乎没有办法检查 preventDefault() 是否已被调用,因为 defaultPrevented 是事件的属性,而不是发射器,因此发射器无法读取。

You have no emitted an event object at all, so there's no method and no property.您根本没有发出事件 object,因此没有方法也没有属性。 You can however easily emulate the DOM dispatchEvent method by creating such an object:但是,您可以通过创建这样的 object 轻松模拟DOM dispatchEvent方法

class MyClass extends EventEmitter{
    close(winID:number): void {
        const event = {
            defaultPrevented: false,
            preventDefault(): void {
                this.defautPrevented = true;
            },
        };
        this.emit('will-quit', event);
        if (!event.defaultPrevented) {
            app.close(winID);
        }
    }
}

No need to install an event listener yourself.无需自己安装事件监听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM