Skip to content

Commit 02b4f85

Browse files
author
Greg Larrenaga
committed
Adding another custom attribute. on-event Version bump 2.1.0. Added notes to the readme.
1 parent 01ffe5e commit 02b4f85

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

Diff for: README.md

+13
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Each custom attribute must have the following methods.
114114
// el === The element in question.
115115
// value === could be a function, a value, anything really. Depends on what you passed the binding.
116116

117+
// NOTE: The scope of this closure is the bound object. so, `this` === the view you are binding
118+
117119
// You add event listeners, do work etc here.
118120
},
119121
unbind : function(el, value) {
@@ -213,4 +215,15 @@ customAttributes.addAttribute('on-click', require('simple-custom-attributes/attr
213215
<div on-click='someMethod'></div>
214216
```
215217

218+
#### on-event='{ event : method, event : method }'
219+
```javascript
220+
var customAttributes = require('simple-custom-attributes');
221+
222+
customAttributes.addAttribute('on-event', require('simple-custom-attributes/attributes/on-event'));
223+
```
224+
225+
```html
226+
<div on-event='{ mouseover : handleMouseOver, mouseout : handleMouseOut }'></div>
227+
```
228+
216229
Let me know how it goes!!!

Diff for: attributes/on-event.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
var parseKeyValuesFromObjectLiteralString = require('parse-keys-and-values-from-object-literal-strings');
4+
5+
module.exports = {
6+
bind: bind,
7+
unbind: unbind
8+
};
9+
10+
function bind(el, value) {
11+
var parsedValues = parseKeyValuesFromObjectLiteralString(value);
12+
13+
parsedValues.forEach(function(parsedValueObj) {
14+
var handler = this[parsedValueObj.value].bind(this);
15+
16+
this[parsedValueObj.value] = handler;
17+
18+
el.addEventListener(parsedValueObj.key, handler, {
19+
capture : false
20+
});
21+
}.bind(this));
22+
}
23+
24+
function unbind(el, value) {
25+
var parsedValues = parseKeyValuesFromObjectLiteralString(value);
26+
27+
parsedValues.forEach(function(parsedValueObj) {
28+
el.removeEventListener(parsedValueObj.key, this[parsedValueObj.value], {
29+
capture : false
30+
});
31+
}.bind(this));
32+
}

Diff for: index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ function register(object, rootElement) {
1616
if(typeof object[element.getAttribute(customAttribute)] === 'function') {
1717
_registerFunction.call(this, object, element, customAttribute);
1818
} else if(object[element.getAttribute(customAttribute)] !== undefined){
19-
this.attributesMap[customAttribute].bind(element, object[element.getAttribute(customAttribute)]);
19+
this.attributesMap[customAttribute].bind.call(object, element, object[element.getAttribute(customAttribute)]);
2020
} else {
21-
this.attributesMap[customAttribute].bind(element, element.getAttribute(customAttribute));
21+
this.attributesMap[customAttribute].bind.call(object, element, element.getAttribute(customAttribute));
2222
}
2323

2424
}.bind(this));
@@ -32,9 +32,9 @@ function unregister(object, rootElement) {
3232
if(typeof object[element.getAttribute(customAttribute)] === 'function') {
3333
_unregisterFunction.call(this, object, element, customAttribute);
3434
} else if(object[element.getAttribute(customAttribute)] !== undefined) {
35-
this.attributesMap[customAttribute].unbind(element, object[element.getAttribute(customAttribute)]);
35+
this.attributesMap[customAttribute].unbind.call(object, element, object[element.getAttribute(customAttribute)]);
3636
} else {
37-
this.attributesMap[customAttribute].unbind(element, element.getAttribute(customAttribute));
37+
this.attributesMap[customAttribute].unbind.call(object, element, element.getAttribute(customAttribute));
3838
}
3939

4040
}.bind(this));
@@ -50,9 +50,9 @@ function _registerFunction(object, element, customAttribute) {
5050

5151
object[element.getAttribute(customAttribute)] = handler;
5252

53-
this.attributesMap[customAttribute].bind(element, handler);
53+
this.attributesMap[customAttribute].bind.call(object, element, handler);
5454
}
5555

5656
function _unregisterFunction(object, element, customAttribute) {
57-
this.attributesMap[customAttribute].unbind(element, object[element.getAttribute(customAttribute)]);
57+
this.attributesMap[customAttribute].unbind.call(object, element, object[element.getAttribute(customAttribute)]);
5858
}

Diff for: package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-custom-attributes",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"main": "index.js",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1"
@@ -18,6 +18,7 @@
1818
"description": "",
1919
"devDependencies": {
2020
"clamp-js": "^0.7.0",
21-
"hammerjs": "^2.0.8"
21+
"hammerjs": "^2.0.8",
22+
"parse-keys-and-values-from-object-literal-strings": "^1.0.0"
2223
}
2324
}

0 commit comments

Comments
 (0)