简体   繁体   English

在node.js中使用事件的最佳方法

[英]Best way using events in node.js

Which is the best approach for listening/launching events in node.js? 在node.js中侦听/启动事件的最佳方法是哪种? I've been testing event launching and listening in node.js by extending the model with EventEmitter and I'm wondering if it has sense this approach since the events are only listened when there is a instance of the model. 我一直在通过使用EventEmitter扩展模型来测试node.js中的事件启动和侦听,我想知道是否有这种方法,因为仅当存在模型实例时才侦听事件。 How can be achieved that events will be listened while the node app is alive?? 如何在节点应用程序处于活动状态时监听事件?

Example for extending the model using eventEmitter. 使用eventEmitter扩展模型的示例。

// myModel.js

var util = require('util');
var events2 = require('events').EventEmitter;
var MyModel = function() {

        events2.call(this);
        // Create a event listener
        this.on('myEvent', function(value) {
            console.log('hi!');
        });
    };

MyModel.prototype.dummyFunction = function(params) {
// just a dummy function.

}

util.inherits(MyModel, events2);
module.exports = MyModel;

EDIT: A more clear question about this would be: how to keep a permanent process that listens the events during the app execution and it has a global scope (something like a running event manager that listens events produced in the app). 编辑:关于此问题,一个更明确的问题是:如何保持一个永久的进程,该进程在应用程序执行期间监听事件,并且具有全局范围(类似于正在运行的事件管理器,它监听应用程序中产生的事件)。 Would be a solution to require the file myModel.js in app.js? 是否需要在app.js中要求文件myModel.js的解决方案? How this kind of things are solved in node.js? 如何在node.js中解决这类问题?

I'm not entirely sure what you mean about events only being active when there is an instance of a model since without something to listen and react to them, events cannot occur. 我不确定您对事件的含义是什么,它仅在存在模型实例时才处于活动状态,因为如果没有可以监听和响应的事件,则事件就不会发生。

Having said that, it is certainly reasonable to: 话虽如此,这样做当然是合理的:

util.inherits(global,EventEmitter)
global.on('myEvent',function(){
    /* do something useful */
});

which would allow you to: 这将使您能够:

global.emit('myEvent')

or even: 甚至:

var ee=new EventEmitter();

ee.on('myEvent',...)

As for how to properly use EventEmitter: it's defined as 至于如何正确使用EventEmitter:它定义为

function EventEmitter() {}

which does not provide for initialization, so it should be sufficient to: 它不提供初始化功能,因此足以:

var Thing=function(){};
util.inherits(Thing,EventEmitter);

which will extend instances of Thing with: 这将扩展Thing的实例:

  • setMaxListeners(num)
  • emit(type,...)
  • addListener(type,listener) -- aliased as on() addListener(type,listener) -别名为on()
  • once(type,listener)
  • removeListener(type,listener)
  • removeAllListeners()
  • listeners(type)

The only possible "gotcha" is that EventEmitter adds its own _events object property to any extended object ( this ) which suggests you should not name any of your own object properties with the same name without unexpected behavior. 唯一可能的“陷阱”是EventEmitter将其自己的_events对象属性添加到任何扩展对象( this ),这建议您不要使用相同的名称来命名自己的任何对象属性,除非出现意外行为。

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

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