RGJS6 Events module.
Members
-
<inner, constant> _options :object
-
Default options for
on()
andoff()
Type:
- object
Properties:
Name Type Description bind
object Binds the
this
for the callback like so:callback.bind(value)
.capture
boolean Event capture option.
once
boolean Event once option.
passive
boolean Event passive option.
removeExisting
boolean Remove existing listeners if the selector and eventName match.
Methods
-
<static> cleanupListeners()
-
Clean up stored listeners.
This method is deprecated and no longers does anything.- Deprecated:
-
- Yes
-
<static> emit(name [, obj] [, detail])
-
Emit an event.
Note: Uses obj.dispatchEvent, runs sync.
Note #2: Does not bubble, is not cancelable.Parameters:
Name Type Argument Default Description name
string Name of the event.
obj
object <optional>
window Optional. Emitter of the event. Defaults to 'window'.
detail
object <optional>
{} Optional. The object containing the details.
Returns:
True if event was fired, false if not.
- Type
- boolean
Example
// Listen for a custom event, then fire it. rgjs.events.on('my_event', window, () => { console.log('My event fired!'); }); rgjs.events.emit('my_event', window);
-
<static> off(eventName, selector [, cb] [, opts])
-
Unregister an event listener.
Note: rgjs can only remove listeners that were registered usingon()
!Parameters:
Name Type Argument Description eventName
string Name of the event to bind (click, scroll, etc). Multiple event names are allowed (separate by space).
selector
* The selector for the element. See rgjs.dom.selectElements().
cb
function <optional>
Optional. The callback for the event.
opts
object <optional>
Optional. See rgjs.events.options for defaults.
Returns:
Number of elements that no longer have a listener.
- Type
- int
Examples
// Remove the listener that calls `this.onclick`. rgjs.events.on('click', 'span.link', this.onclick); rgjs.events.off('click', 'span.link', this.onclick);
// Remove all click listeners bound to `span.link` (in this case: `this.onclick_1` and `-.onclick_2`). rgjs.events.on('click', 'span', this.onclick_1, {bind: this}); rgjs.events.on('click', 'span.link', this.onclick_2, {bind: this}); rgjs.events.off('click', 'span.link');
-
<static> on(eventName, selector [, cb] [, opts])
-
Register an event listener.
Note: Use option {bind: this} instead of cb.bind(this):on('click', '.link', clickHandler, {bind: this})
.
This ensures that rgjs can find the event listener when callingoff('click', '.link', clickHandler)
.Parameters:
Name Type Argument Description eventName
string Name of the event to bind (click, scroll, etc). Multiple event names are allowed (separate by space).
selector
* The selector for the element. See rgjs.dom.selectElements().
cb
function <optional>
The callback for the event.
opts
object <optional>
Optional. See rgjs.events.options for defaults.
Returns:
Number of elements that now have a listener.
- Type
- int
Examples
// Call `this.onclick` function when `span.link` is clicked. rgjs.events.on('click', 'span.link', this.onclick);
// Like the above, but bind bind `onclick` to `this` instead of the target element. rgjs.events.on('click', 'span.link', this.onclick, {bind: this});
// Events are bound as passive by default, so if you don't want this because you need to, for example, call `event.preventDefault()`: rgjs.events.on('click', 'span.link', this.onclick, {passive: false});
-
<static> preventDefault(event)
-
Prevent default.
Parameters:
Name Type Description event
event The event.
- Deprecated:
-
- Yes
-
<static> preventDefaultStopPropagation(event)
-
Prevent default and stop propagation.
Parameters:
Name Type Description event
event The event.
- Deprecated:
-
- Yes
-
<static> stopPropagation(event)
-
Stop propagation.
Parameters:
Name Type Description event
event The event.
- Deprecated:
-
- Yes
-
<static> trigger(name, selector [, opts])
-
Trigger a standard event on an element.
Parameters:
Name Type Argument Default Description name
string Name of the event to trigger.
selector
* The selector for the element. See rgjs.dom.selectElements().
opts
object <optional>
{} Options are optional.
Properties
Name Type Argument Default Description bubbles
boolean <optional>
true Passed to element.dispatchEvent()
cancelable
boolean <optional>
true Passed to element.dispatchEvent()
Example
// Trigger an event rgjs.events.trigger('change', input_element);