Bean: Framework-Agnostic Event Manager
Bean is a small, fast, and framework-agnostic event manager designed for desktop, mobile, and touch-based browsers, originating from an earlier era of web development (circa 2010-2012). The current stable version is 1.0.15, which was released over a decade ago. The project is no longer actively maintained or developed, making it an abandoned package. It offered a concise API for event handling, including `on`, `one`, `off`, `clone`, and `fire`, along with support for event delegation and namespacing. A key differentiator was its standalone nature, providing event management capabilities without the overhead of larger JavaScript libraries like jQuery.
Common errors
-
Uncaught TypeError: bean.on is not a function
cause The 'bean' object was not correctly loaded or is not available in the global scope/module context.fixEnsure the 'bean' script is included in your HTML before your script, or that `require('bean')` is correctly used in a CommonJS environment. For modern front-end development, this package is not recommended. -
Delegated event not firing for focus/blur on child elements.
cause The `focus`, `blur`, and `submit` events do not naturally bubble in the DOM, preventing event delegation with Bean (and most other event libraries).fixAttach event listeners directly to the specific elements that need to react to `focus` or `blur`. For `submit`, attach the listener directly to the `<form>` element.
Warnings
- deprecated The `bean.add()` method, while still present in v1.0.15, is a legacy handler-adding interface from pre-v1.x. Its argument order differs from `bean.on()` for delegated events and it may have undefined behavior in modern contexts due to the package's abandonment.
- gotcha Event delegation using `bean.on()` does not work for `focus`, `blur`, and `submit` events due to browser DOM model limitations. These events will not bubble or delegate as expected.
- breaking When migrating from pre-v1.x versions of Bean to v1.x (including 1.0.15), the behavior of namespace matching for `bean.fire()` and `bean.off()` changed from an 'OR' logic to an 'AND' logic. This means `bean.fire(element, 'click.fat.foo')` will only trigger handlers explicitly bound to *both* 'fat' and 'foo' namespaces.
Install
-
npm install bean -
yarn add bean -
pnpm add bean
Imports
- bean
import bean from 'bean'
const bean = require('bean') - bean.on
import { on } from 'bean'const bean = require('bean'); bean.on(element, 'click', handler); - bean.fire
import { fire } from 'bean'const bean = require('bean'); bean.fire(element, 'click');
Quickstart
const bean = require('bean');
const button = document.createElement('button');
button.textContent = 'Click me';
document.body.appendChild(button);
bean.on(button, 'click', function (e) {
console.log('Button clicked!', e.type, e.target);
});
const delegatedContainer = document.createElement('div');
delegatedContainer.id = 'container';
document.body.appendChild(delegatedContainer);
const paragraph1 = document.createElement('p');
paragraph1.className = 'content-item';
paragraph1.textContent = 'Paragraph 1';
delegatedContainer.appendChild(paragraph1);
const paragraph2 = document.createElement('p');
paragraph2.className = 'content-item';
paragraph2.textContent = 'Paragraph 2';
delegatedContainer.appendChild(paragraph2);
bean.on(delegatedContainer, 'click', '.content-item', function (e) {
console.log('Delegated click on:', e.target.textContent);
});
// Example of namespaced event
bean.on(button, 'mouseover.highlight', function() {
console.log('Button mouseover (namespaced)');
});
// Fire a namespaced event
setTimeout(() => {
bean.fire(button, 'mouseover.highlight');
}, 1000);
// Clean up an event
setTimeout(() => {
bean.off(button, 'click');
console.log('Click handler removed from button.');
}, 2000);