Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Events Model without using DOM events in JavaScript

I'm new to JavaScript and programming in general, and I have some questions about objects and events.

Say I have an object:

var computer = {     keyboard: {} } 

What I'm looking for is a way to register events to the keyboard object:

computer.keyboard.registerEvent( "keyEscape" ); 

Fire the event:

computer.keyboard.dispatchEvent( "keyEscape" ); 

And create event handlers:

computer.keyboard.addEventListener( "keyEscape", function() {...} ); 

I know how to do this with DOM elements but not objects. Is this something that can be done in JavaScript (maybe with the help of JQuery)?

Even the slightest bit of guidance would be appreciated greatly.

like image 812
NTDave Avatar asked Mar 09 '13 07:03

NTDave


People also ask

What are custom events in JavaScript?

A custom event can be created using the CustomEvent constructor: const myEvent = new CustomEvent("myevent", { detail: {}, bubbles: true, cancelable: true, composed: false, }); As shown above, creating a custom event via the CustomEvent constructor is similar to creating one using the Event constructor.

Which method can be used to let JavaScript work with events?

Many different methods can be used to let JavaScript work with events: HTML event attributes can execute JavaScript code directly. HTML event attributes can call JavaScript functions. You can assign your own event handler functions to HTML elements.

Is events can be triggered by JavaScript?

HTML events are handled by JavaScript. When an event occurs, it requires some action to be taken. This action can be accomplished through JavaScript event handlers. In addition to JavaScript, jQuery which is equivalent to JavaScript in terms of functionality can also be used to trigger events in a HTML document.


1 Answers

If you want to make a completely stand alone event system without relying on DOM events you can have something like this using reactor pattern

function Event(name){   this.name = name;   this.callbacks = []; } Event.prototype.registerCallback = function(callback){   this.callbacks.push(callback); }  function Reactor(){   this.events = {}; }  Reactor.prototype.registerEvent = function(eventName){   var event = new Event(eventName);   this.events[eventName] = event; };  Reactor.prototype.dispatchEvent = function(eventName, eventArgs){   this.events[eventName].callbacks.forEach(function(callback){     callback(eventArgs);   }); };  Reactor.prototype.addEventListener = function(eventName, callback){   this.events[eventName].registerCallback(callback); }; 

Use it like DOM events model

var reactor = new Reactor();  reactor.registerEvent('big bang');  reactor.addEventListener('big bang', function(){   console.log('This is big bang listener yo!'); });  reactor.addEventListener('big bang', function(){   console.log('This is another big bang listener yo!'); });  reactor.dispatchEvent('big bang'); 

Live at JSBin

like image 126
Mohsen Avatar answered Sep 23 '22 19:09

Mohsen