Skip to content

Instantly share code, notes, and snippets.

@wallacemaxters
Last active May 9, 2018 16:59
Show Gist options
  • Save wallacemaxters/2c3a00e7bfac1182edd9ea19095c3dc4 to your computer and use it in GitHub Desktop.
Save wallacemaxters/2c3a00e7bfac1182edd9ea19095c3dc4 to your computer and use it in GitHub Desktop.
Sugestão de manipulação de events para a biblioteca https://github.com/brcontainer/jot.js
(function (window) {
"use strict";
var forEach = Array.prototype.forEach;
var Event = function (nodeList) {
this.nodeList = nodeList;
};
var proto = Event.prototype;
proto.on = function on(event, callback) {
return this.apply(function (el) {
el.addEventListener(event, callback, false);
});
};
proto.off = function off(event, callback) {
return this.apply(function (el) {
callback ? el.removeEventListener(event, callback) : el.removeEventListener(event);
});
};
proto.prop = function prop(name, value) {
var callback = function (el) {
if (typeof value === 'function') {
value = value(el[name], el);
}
if ([undefined, null, false].indexOf(value) >= 0) {
el.removeAttribute(name);
} else {
el.setAttribute(name, value);
}
};
return this.apply(callback);
};
prop.apply = function (callback) {
forEach.call(this.nodeList, callback);
return this;
};
window.JotEvent = Event;
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment