npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dom-emitter

v0.7.0

Published

Manage the events of a DOM element

Downloads

4

Readme

Dom Emitter

Manage the events of a DOM element.

Features

  • stores all listeners making cleanup easy
  • one DOM binding per event type
  • intuitive delegation
  • emits proper DOM events
  • convenient method binding
  • efficient context binding (no fn.bind(this))

Examples

var body = new DomEmitter(document.body)
body.on('click', console.log)
body.emit('click', {x:50,y:112})
// => {type: 'click', x:50, y:112, ...}

It is also has a simple system for inferring methods from the name of the event:

function Button () {
	this.view = document.createElement('button')
	this.events = new DomEmitter(this.view, this)
	this.events.on('click')
}
Button.prototype.onClick = console.log
new Button().events.emit('click', {x:50,y:112})
// => {type: 'click', x:50, y:112, ...}

Delegation. leave a space then write a CSS query:

body.on('click > div.button') // infers "onClick"

Will only be triggered if a click occurs within a direct child of document.body that has a tagName of "div" and a "button" class.

Naming delegated functions can be a bit tricky so sometimes its more readable to declare them in an object:

body.on({
	'click > div.button': console.log,
	'mousedown > div.button': function(e){
		e.delegate.style.backgroundColor = '#888'
	},
	'login': function(e){
		alert('Welcome!')
	}
})

Getting Started

With component

$ component install jkroso/dom-emitter

With npm

$ npm install jkroso/dom-emitter --save

API

var DomEmitter = require('dom-emitter')

DomEmitter()

Initialize a DomEmitter. If you provide a context then that will be the source of implies methods. It will also be this inside handlers.

new DomEmitter(document.body, {
  onClick: console.log  
})

DomEmitter.on(type:String, [method]:String)

Bind to type with optional method. When method is undefined it inferred from type. Delegation is can be specified in type

 events.on('click', 'onClick')
 events.on('click') // implies "onClick"
 events.on('click', function (e) {})
 events.on('click .ok') // delegates to `.ok`

DomEmitter.off(type:String, [method]:String)

Remove a single behavior

All the following are equivalent:

events.off('click', 'onClick')
events.off('click') // implies 'onClick'
events.off('click', events.onClick)

DomEmitter.once()

Add listener but remove it after one call

DomEmitter.emit(topic:String, [data]:Any)

Create a DOM event and send it down to the DomEmitter's target. Any data you pass will be merged with the event object

manager.emit('mousedown')
manager.emit('login', {user: user})
manager.emit('keydown', {key: 'enter'})

DomEmitter.clear([topic]:String)

Remove all bound functions. Optionally limited to a certain topic

this.clear() // all
this.clear('click') // just click handlers