Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 689cd70fea359bf4e116ca49e36a32404231b6de @tj tj committed Oct 6, 2012
Showing with 120 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +11 −0 Makefile
  3. +37 −0 Readme.md
  4. +10 −0 component.json
  5. +36 −0 index.js
  6. +24 −0 test/index.html
@@ -0,0 +1,2 @@
+components
+build
@@ -0,0 +1,11 @@
+
+build: components index.js
+ @component build
+
+components: component.json
+ @component install --dev
+
+clean:
+ rm -fr build components
+
+.PHONY: clean
@@ -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
@@ -0,0 +1,10 @@
+{
+ "name": "event",
+ "repo": "component/event",
+ "description": "Event binding component",
+ "version": "0.0.1",
+ "keywords": ["event", "events"],
+ "scripts": [
+ "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);
+ }
+};
@@ -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>

0 comments on commit 689cd70

Please sign in to comment.