Last active
October 12, 2015 09:58
-
-
Save brettz9/4009884 to your computer and use it in GitHub Desktop.
bottomDollar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Currently non-functional lite jQuery drop-in replacement. | |
"Bottom Dollar" as name is to express there being a bargain for the | |
amount of code, and the "_$" convention also spells this out). | |
@ todos | |
1. Replace __noSuchMethod__ with Proxy | |
*/ | |
var _$; | |
if (typeof _$ === 'undefined') { | |
_$ = function _$ (obj) { | |
switch (typeof obj) { | |
case 'string': | |
if (obj.charAt(0) === '<') { // We could add detection from fragments | |
this[0] = new DOMParser().parseFromString(obj, 'text/html').documentElement; | |
} | |
else { | |
Array.from(document.querySelectorAll(obj)).forEach(function (elem, idx) { | |
this[idx] = elem; | |
}, this); | |
} | |
return this; | |
case 'function': | |
window.addEventListener('DOMContentLoaded', obj); | |
return this; | |
} | |
}; | |
$.ajax = function (opts) { | |
var data = opts.data, | |
req = new XMLHttpRequest(); | |
data = data ? Object.keys(data).map(function (key) { | |
encodeURIComponent(key) + '=' + encodeURIComponent(data[key]); | |
}).join('&') : null; | |
req.onreadystatechange = function () { | |
if (req.readyState === 4) { | |
if (req.status === 200) { | |
opts.success(req.responseText, req.status, req); // Not identical to jQuery | |
} | |
else { | |
opts.error(req, req.status); // Not identical to jQuery | |
} | |
} | |
}; | |
req.open(opts.type, opts.url, true); | |
req.send(data); | |
}; | |
_$.__noSuchMethod__ = function (method, args) { // ECMAScript version?? | |
if ((/^click|dblclick|mousedown|mouseup|mouseover|mousemove|mouseout|keydown|keypress|keyup|load|unload|abort|error|resize|scroll|select|change|submit|reset|focus|blur|focusin|focusout$/). | |
test(method)) { | |
var i = 0; | |
while (this[i]) { | |
this[i++].addEventListener(method, args[0], false); | |
} | |
} | |
throw 'Calling an recognized method on _$'; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment