Created
March 29, 2010 18:34
-
-
Save rlr/348220 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Proof of Concept | |
* webkit specific library that (eventually) implements the jQuery API ~ish | |
*/ | |
(function( window, undefined ) { | |
var webkitQuery = function (selector, context) { | |
return new WebkitQuery(selector, context); | |
}; | |
function WebkitQuery(selector, context){ | |
// Handle $(DOMElement) | |
if ( selector.nodeType ) { | |
this.context = this[0] = selector; | |
this.length = 1; | |
return this; | |
} | |
// assume context is a dom element for now... | |
var nodelist = (context || document).querySelectorAll(selector); | |
for (var i=0, l=nodelist.length; i<l; i++) { | |
this[i] = nodelist[i]; | |
} | |
this.length = nodelist.length; | |
return this; | |
} | |
webkitQuery.fn = { | |
length: 0, | |
each: function(callback) { | |
for (var i=0, l=this.length; i<l; i++) { | |
callback.call(this[i], i, this[i]); | |
} | |
return this; | |
}, | |
bind: function(type, callback) { | |
var $this = this; | |
this.each(function(){ | |
if (typeof type === 'object') { | |
for ( var key in type ) { | |
$this.bind(key, type[key]); | |
} | |
} else { | |
this.addEventListener(type, callback, false); | |
} | |
}); | |
return this; | |
}, | |
click: function(callback) { | |
return this.bind('click', callback); | |
} | |
}; | |
WebkitQuery.prototype = webkitQuery.fn; | |
window.webkitQuery = window.$ = webkitQuery; | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment