Skip to content

Instantly share code, notes, and snippets.

@L8D

L8D/zync.js

Last active Aug 29, 2015
Embed
What would you like to do?
;(function(root, factory) {
if (typeof define === 'function') {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.Zync = root.$ = factory();
}
})(this, function() {
function fromMarkup(markup) {
var div = document.createElement('div');
div.innerHTML = markup;
return div.childNodes[0];
}
function select(selector) {
if (typeof selector === 'string') {
if (selector[0] === '<') {
return fromMarkup(selector);
} else {
return document.querySelector(selector);
}
} else if (selector instanceof Zync) {
return selector.el;
} else {
return selector;
}
}
function Zync(selector) {
if (!(this instanceof Zync)) {
return new Zync(selector);
}
this.el = select(selector);
}
Zync.fn = Zync;
var proto = Zync.prototype;
proto.toString = function() {
return '' + this.el;
};
proto.valueOf = function() {
return +this.el;
};
proto.on = function(event, callback) {
this.el.addEventListener(event, callback);
};
proto.off = function(event, callback) {
this.el.removeEventListener(event, callback);
};
proto.emit = function(event, value) {
return this.el.dispatchEvent(new Event(event, value));
};
proto.find = function(selector) {
return this.el.querySelector(selector);
};
proto.html = function(html) {
if (html == null) {
return this.el.innerHTML;
} else {
return this.el.innerHTML = html;
}
};
proto.css = function(attr, value) {
if (value == null) {
return this.el.style[attr];
} else {
return this.el.style[attr] = value;
}
};
proto.attr = function(attr, value) {
if (value == null) {
return this.el.getAttribute(attr);
} else {
return this.el.setAttribute(attr, value);
}
};
return Zync;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.