Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 689cd70fea359bf4e116ca49e36a32404231b6de 0 parents
@tj tj authored
2  .gitignore
@@ -0,0 +1,2 @@
+components
+build
11 Makefile
@@ -0,0 +1,11 @@
+
+build: components index.js
+ @component build
+
+components: component.json
+ @component install --dev
+
+clean:
+ rm -fr build components
+
+.PHONY: clean
37 Readme.md
@@ -0,0 +1,37 @@
+
+# event
+
+ Element event binding component.
+
+## Installation
+
+ $ component install component/event
+
+## Example
+
+```js
+var events = require('event');
+var a = document.querySelector('a');
+
+function onclick(e) {
+ e.preventDefault();
+ console.log(e.target);
+ events.unbind(a, 'click', onclick);
+}
+
+events.bind(a, 'click', onclick);
+```
+
+## API
+
+### .bind(el, type, callback, [capture])
+
+ Bind to `el`'s event `type` with `callback`.
+
+### .unbind(el, type, callback, [capture])
+
+ Unbind `el`'s event `type` `callback`.
+
+## License
+
+ MIT
10 component.json
@@ -0,0 +1,10 @@
+{
+ "name": "event",
+ "repo": "component/event",
+ "description": "Event binding component",
+ "version": "0.0.1",
+ "keywords": ["event", "events"],
+ "scripts": [
+ "index.js"
+ ]
+}
36 index.js
@@ -0,0 +1,36 @@
+
+/**
+ * Bind `el` event `type` to `fn`.
+ *
+ * @param {Element} el
+ * @param {String} type
+ * @param {Function} fn
+ * @param {Boolean} capture
+ * @api public
+ */
+
+exports.bind = function(el, type, fn, capture){
+ if (el.addEventListener) {
+ el.addEventListener(type, fn, capture);
+ } else {
+ el.attachEvent('on' + type, fn);
+ }
+};
+
+/**
+ * Unbind `el` event `type`'s callback `fn`.
+ *
+ * @param {Element} el
+ * @param {String} type
+ * @param {Function} fn
+ * @param {Boolean} capture
+ * @api public
+ */
+
+exports.unbind = function(el, type, fn, capture){
+ if (el.removeEventListener) {
+ el.removeEventListener(type, fn, capture);
+ } else {
+ el.detachEvent('on' + type, fn);
+ }
+};
24 test/index.html
@@ -0,0 +1,24 @@
+<html>
+ <head>
+ <title>Event</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ </head>
+ <body>
+ <a href="/something">link</a>
+
+ <script src="../build/build.js"></script>
+
+ <script>
+ var events = require('event');
+ var a = document.querySelector('a');
+
+ function onclick(e) {
+ e.preventDefault();
+ console.log(e.target);
+ events.unbind(a, 'click', onclick);
+ }
+
+ events.bind(a, 'click', onclick);
+ </script>
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.