Created
November 1, 2014 23:10
-
-
Save loonison123/0809f0e0fb09870dc791 to your computer and use it in GitHub Desktop.
Node.js emit events by inheriting EventEmitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // csvHelper.js | |
| var events = require('events'); | |
| function CsvHelper () { | |
| this.config = null; | |
| this.data = null; | |
| // Need to instantiate EventEmitter constructor | |
| events.EventEmitter.call(this); | |
| } | |
| // Copy event functions to this prototype | |
| CsvHelper.prototype.__proto__ = events.EventEmitter.prototype; | |
| // Custom prototyped functions | |
| CsvHelper.prototype.parse = function () { | |
| // Parse csv data into usable objects | |
| if (parseSuccessful) { | |
| this.emit('csvHelper.parseSuccessful', data); | |
| } else { | |
| this.emit('csvHelper.parseFailed', err); | |
| } | |
| } | |
| CsvHelper.prototype.getHeaders = function () { | |
| } | |
| // Use the data | |
| var csvHelper = new CsvHelper(); | |
| csvHelper.on('csvHelper.parseSuccessful', function (data) { | |
| // Tell the user it parsed successfully | |
| }); | |
| csvHelper.parse(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment