├── .gitignore ├── History.md ├── LICENSE ├── Makefile ├── Readme.md ├── component.json ├── index.js ├── package.json └── test └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | components 2 | build 3 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 0.2.1 / 2022-12-23 2 | ================== 3 | 4 | * package: add `license` to `package.json` 5 | * add missing LICENSE file 6 | 7 | 0.2.0 / 2016-09-22 8 | ================== 9 | 10 | * [[`f1c2c3d2b1`](https://github.com/component/component-event/commit/f1c2c3d2b1)] - make requireable without `window` (Nathan Rajlich) 11 | 12 | 0.1.4 / 2014-05-27 13 | ================== 14 | 15 | * component, package: add "browser" as a keyword 16 | * package: remove "repo" field 17 | 18 | 0.1.3 / 2014-02-10 19 | ================== 20 | 21 | * package: rename module to "component-event" 22 | 23 | 0.1.2 / 2013-12-01 24 | ================== 25 | 26 | * package: add "repository" field 27 | * check if addEventListener method is supported by the browser, just once 28 | * test: update to test in IE7+ 29 | 30 | 0.1.1 / 2013-08-16 31 | ================== 32 | 33 | * fix: default `capture` to `false` 34 | 35 | 0.1.0 / 2012-12-18 36 | ================== 37 | 38 | * add returning of the callback function 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2012 TJ Holowaychuk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | build: components index.js 3 | @component build 4 | 5 | components: component.json 6 | @component install --dev 7 | 8 | clean: 9 | rm -fr build components 10 | 11 | .PHONY: clean 12 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # event 3 | 4 | Element event binding component. 5 | 6 | ## Installation 7 | 8 | $ component install component/event 9 | 10 | ## Example 11 | 12 | ```js 13 | var events = require('event'); 14 | var a = document.querySelector('a'); 15 | 16 | function onclick(e) { 17 | e.preventDefault(); 18 | console.log(e.target); 19 | events.unbind(a, 'click', onclick); 20 | } 21 | 22 | events.bind(a, 'click', onclick); 23 | ``` 24 | 25 | ## API 26 | 27 | ### .bind(el, type, callback, [capture]) 28 | 29 | Bind to `el`'s event `type` with `callback`, 30 | returns the `callback` passed. 31 | 32 | ### .unbind(el, type, callback, [capture]) 33 | 34 | Unbind `el`'s event `type` `callback`, 35 | returns the `callback` passed. 36 | 37 | ## License 38 | 39 | MIT 40 | -------------------------------------------------------------------------------- /component.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "event", 3 | "repo": "component/event", 4 | "description": "Event binding component", 5 | "version": "0.2.1", 6 | "keywords": [ 7 | "browser", 8 | "event", 9 | "events" 10 | ], 11 | "scripts": [ 12 | "index.js" 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var bind, unbind, prefix; 2 | 3 | function detect () { 4 | bind = window.addEventListener ? 'addEventListener' : 'attachEvent'; 5 | unbind = window.removeEventListener ? 'removeEventListener' : 'detachEvent'; 6 | prefix = bind !== 'addEventListener' ? 'on' : ''; 7 | } 8 | 9 | /** 10 | * Bind `el` event `type` to `fn`. 11 | * 12 | * @param {Element} el 13 | * @param {String} type 14 | * @param {Function} fn 15 | * @param {Boolean} capture 16 | * @return {Function} 17 | * @api public 18 | */ 19 | 20 | exports.bind = function(el, type, fn, capture){ 21 | if (!bind) detect(); 22 | el[bind](prefix + type, fn, capture || false); 23 | return fn; 24 | }; 25 | 26 | /** 27 | * Unbind `el` event `type`'s callback `fn`. 28 | * 29 | * @param {Element} el 30 | * @param {String} type 31 | * @param {Function} fn 32 | * @param {Boolean} capture 33 | * @return {Function} 34 | * @api public 35 | */ 36 | 37 | exports.unbind = function(el, type, fn, capture){ 38 | if (!unbind) detect(); 39 | el[unbind](prefix + type, fn, capture || false); 40 | return fn; 41 | }; 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "component-event", 3 | "description": "Event binding component", 4 | "version": "0.2.1", 5 | "license": "MIT", 6 | "keywords": [ 7 | "browser", 8 | "event", 9 | "events" 10 | ], 11 | "component": { 12 | "scripts": { 13 | "event/index.js": "index.js" 14 | } 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/component/event.git" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Event 4 | 5 | 6 | 7 | link 8 | 9 | 10 | 11 | 32 | 33 | --------------------------------------------------------------------------------