logo

/Code Snippet|v2.3.3

Class

new CustomEvents()customEvents.jsline 27

EXAMPLES
// ES6
import CustomEvents from 'tui-code-snippet/customEvents/customEvents';

// CommonJS
const CustomEvents = require('tui-code-snippet/customEvents/customEvents');

Static Methods

mixin(func)customEvents.jsline 60

Mixin custom events feature to specific constructor

PARAMETERS
NameTypeDescription

func

function

constructor

EXAMPLES
//ES6
import CustomEvents from 'tui-code-snippet/customEvents/customEvents'; 

// CommonJS
const CustomEvents = require('tui-code-snippet/customEvents/customEvents'); 

function Model() {
    this.name = '';
}
CustomEvents.mixin(Model);

const model = new Model();
model.on('change', function() { this.name = 'model'; }, this);
model.fire('change');
alert(model.name); // 'model';

Instance Methods

fire(eventName)customEvents.jsline 506

Fire custom event

PARAMETERS
NameTypeDescription

eventName

string

name of custom event

getListenerLength(eventName)customEvents.jsline 577

Return a count of events registered.

PARAMETERS
NameTypeDescription

eventName

string

Custom event name

RETURNS:
{

number

} - number of event

hasListener(eventName)customEvents.jsline 568

Return whether at least one of the handlers is registered in the given
event name.

PARAMETERS
NameTypeDescription

eventName

string

Custom event name

RETURNS:
{

boolean

} - Is there at least one handler in event name?

invoke(eventName, data)customEvents.jsline 538

Fire a event and returns the result of operation 'boolean AND' with all
listener's results.

PARAMETERS
NameTypeDescription

eventName

string

Custom event name

data

*

Data for event

RETURNS:
{

boolean

} - The result of operation 'boolean AND'
EXAMPLES
const map = new Map();
map.on({
  'beforeZoom': function() {
    // It should cancel the 'zoom' event by some conditions.
    if (that.disabled && this.getState()) {
      return false;
    }
    return true;
  }
});

if (this.invoke('beforeZoom')) {    // check the result of 'beforeZoom'
  // if true,
  // doSomething
}

off(eventName, handler)customEvents.jsline 485

Unbind custom events

PARAMETERS
NameTypeDescription

eventName

stringobjectfunction

event name or context or
{name: handler} pair object or handler function

handler

function

handler function

EXAMPLES
//-- #1. Get Module --//
// ES6
import CustomEvents from 'tui-code-snippet/customEvents/customEvents'; 

// CommonJS
const CustomEvents = require('tui-code-snippet/customEvents/customEvents'); 

//-- #2. Use method --//
// # 2.1 off by event name
CustomEvents.off('onload');

// # 2.2 off by event name and handler
CustomEvents.off('play', handler);

// # 2.3 off by handler
CustomEvents.off(handler);

// # 2.4 off by context
CustomEvents.off(myObj);

// # 2.5 off by context and handler
CustomEvents.off(myObj, handler);

// # 2.6 off by context and event name
CustomEvents.off(myObj, 'onload');

// # 2.7 off by an Object.<string, function> that is {eventName: handler}
CustomEvents.off({
  'play': handler,
  'pause': handler2
});

// # 2.8 off the all events
CustomEvents.off();

on(eventName, handler, context)customEvents.jsline 238

Bind event handlers

PARAMETERS
NameTypeDescription

eventName

stringObject

custom
event name or an object {eventName: handler}

handler

[ functionobject ]

handler function or context

context

[ object ]

context for binding
//-- #1. Get Module --//
// ES6
import CustomEvents from 'tui-code-snippet/customEvents/customEvents';

once(eventName, handler, context)customEvents.jsline 263

Bind one-shot event handlers

PARAMETERS
NameTypeDescription

eventName

stringObject

custom
event name or an object {eventName: handler}

handler

[ functionobject ]

handler function or context

context

[ object ]

context for binding

Resizable