GithubHelp home page GithubHelp logo

code-review-spot / eventbus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from krasimir/eventbus

0.0 3.0 0.0 98 KB

Simple JavaScript class for managing events

Home Page: http://krasimirtsonev.com/blog/article/javascript-managing-events-dispatch-listen

JavaScript 100.00%

eventbus's Introduction

Simple JavaScript class for managing events in JavaScript

API

// adds event listener
// @type - string
// @callback - function
// @scope - the scope where the @callback is defined
EventBus.addEventListener(type, callback, scope)

// removes event listener
// @type - string
// @callback - function
// @scope - the scope where the @callback is defined
EventBus.removeEventListener(type, callback, scope)

// checks for listener
// @type - string
// @callback - function
// @scope - the scope where the @callback is defined
EventBus.hasEventListener(type, callback, scope)

// dispatch an event
// @type - string
// @target - the caller
// @args - pass as many arguments as you want
EventBus.dispatch(type, target, args ...)

// for debugging purpose, it just prints out the added listeners
EventBus.getEvents()

Usage

function myFunction(event) {
    console.log("myFunction type=" + event.type);
}
EventBus.addEventListener("my_function_event", myFunction);
EventBus.dispatch("my_function_event");

Keeping the scope

var TestClass1 = function() {
    this.className = "TestClass1";
    this.callback = function(event) {
        console.log(this.className + " = type:" + event.type + " / dispatcher:" + event.target.className);
    }
};
var TestClass2 = function() {
    this.className = "TestClass2";
    this.dispatchOurEvent = function() {
        EventBus.dispatch("callback_event", this);
    }
};
var t1 = new TestClass1();
var t2 = new TestClass2();
EventBus.addEventListener("callback_event", t1.callback, t1);
t2.dispatchOurEvent();

Passing additional parameters

var TestClass1 = function() {
    this.className = "TestClass1";
    this.doSomething = function(event, param1, param2) {
        console.log(this.className + ".doSomething");
        console.log("type=" + event.type);
        console.log("params=" + param1 + param2);
        console.log("coming from=" + event.target.className);
    }
};
var TestClass2 = function() {
    this.className = "TestClass2";
    this.ready = function() {
        EventBus.dispatch("custom_event", this, "javascript events", " are really useful");
    }
};

var t1 = new TestClass1();
var t2 = new TestClass2();

EventBus.addEventListener("custom_event", t1.doSomething, t1);
t2.ready();

eventbus's People

Contributors

alexandernst avatar arjun024 avatar code-review-bot avatar l-catallo avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.