eventbusjs

WebJar for eventbusjs

Лицензия

Лицензия

MIT
Категории

Категории

EventBus Библиотеки уровня приложения Messaging
Группа

Группа

org.webjars.npm
Идентификатор

Идентификатор

eventbusjs
Последняя версия

Последняя версия

0.1.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

eventbusjs
WebJar for eventbusjs
Ссылка на сайт

Ссылка на сайт

http://webjars.org
Система контроля версий

Система контроля версий

https://github.com/krasimir/EventBus

Скачать eventbusjs

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/org.webjars.npm/eventbusjs/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>eventbusjs</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/eventbusjs/
implementation 'org.webjars.npm:eventbusjs:0.1.0'
// https://jarcasting.com/artifacts/org.webjars.npm/eventbusjs/
implementation ("org.webjars.npm:eventbusjs:0.1.0")
'org.webjars.npm:eventbusjs:jar:0.1.0'
<dependency org="org.webjars.npm" name="eventbusjs" rev="0.1.0">
  <artifact name="eventbusjs" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='eventbusjs', version='0.1.0')
)
libraryDependencies += "org.webjars.npm" % "eventbusjs" % "0.1.0"
[org.webjars.npm/eventbusjs "0.1.0"]

Зависимости

Библиотека не имеет зависимостей. Это самодостаточное приложение, которое не зависит ни от каких других библиотек.

Модули Проекта

Данный проект не имеет модулей.

Simple JavaScript class for managing events in JavaScript

Installation

In a browser

Download eventbus.min.js and add it to your page.

In Node

npm i eventbusjs -S

And then in your code:

var EventBus = require('eventbusjs');

API

addEventListener

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

removeEventListener

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

hasEventListener

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

dispatch

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

getEvents

For debugging purpose, it 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();

Example of usage EventBus.removeEventListener

To remove EventListener you have to pass same instance of callback

/* Wrong - callback functions are different instances */
EventBus.addEventListener('EXAMPLE_EVENT', function() {
    console.log('example callback');
});
EventBus.removeEventListener('EXAMPLE_EVENT', function() {
    console.log('example callback');
});

/* Correct - callback function is the same instance */
var handler = function() {
    console.log('example callback');
};
EventBus.addEventListener('EXAMPLE_EVENT', handler);
EventBus.removeEventListener('EXAMPLE_EVENT', handler);

Версии библиотеки

Версия
0.1.0