Skip to content

Instantly share code, notes, and snippets.

@cdcabrera
Last active January 4, 2016 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdcabrera/d812cc14123a060d32ef to your computer and use it in GitHub Desktop.
Save cdcabrera/d812cc14123a060d32ef to your computer and use it in GitHub Desktop.
Custom Application Programming Interface, make your own.
/*
* referenced https://github.com/dciccale/ki.js
* referenced jQuery 2005, 2012 jQuery Foundation, Inc. | jquery.org/license
*/
//-- make your own api
(function(window, undefined){
var _api = 'yourapinamehere', _int, _prototype, _extension;
//-- prototype methods (applied context)
_prototype =
{
//-- apply context
go : function(selector, context)
{
if(selector)
{
this.context = (context||window.document);
if( "" + selector === selector )
{
this.selector = selector;
[].push.apply(this, [].slice.call((this.context).querySelectorAll(this.selector), 0));
}
else if( selector.nodeType )
{
[].push.call(this, selector);
}
else if( _int.type(selector) == 'htmlcollection' || _int.type(selector) =='nodelist' )
{
[].push.apply(this, [].slice.call(selector, 0));
}
else if( /^f/.test(typeof selector) )
{
this.ready(selector);
}
}
return this;
},
ready: function( callback )
{
return /^f/.test(typeof callback) ? /c/.test(document.readyState) ? setTimeout(callback) : document.addEventListener('DOMContentLoaded', callback, false) : this.go(callback);
},
//-- add your own contextual methods...
helloWorld : function( value )
{
console.log(this[0]);
console.log(value);
}
}
//-- extended methods (no context)
_extension =
{
type: function(obj)
{
return (obj.__array__ || Object.prototype.toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase());
},
//-- add your own methods
helloWorldNoContext : function( value )
{
console.log(this[0]);
console.log(value);
}
};
//-- custom initilizer
_int = (function( api, prot, ext, intl )
{
if(typeof api != 'string'){return null;}
var o = function(){}, m;
window[api] = m = function(a,b){ return (new o()).__intialize__(a,b); };
m.extend = function(){return(function __a(x){var b=x.shift(),w='strict',q='undefined',r='deep',t='';if(typeof b==='string'||typeof b==='boolean'){t=(b===true)?r:b;b=x.shift();if(t=='defaults'){b=__a([{},b]);t=w}}for(var i=0,c=x.length;i<c;i++){var y=x[i];for(var z in y){if(t==r&&typeof y[z]==='object'&&typeof b[z]!==q){__a([t,b[z],y[z]])}else if(t!=w||(t==w&&typeof b[z]!==q)){b[z]=y[z]}}}return b})(Array.prototype.slice.call(arguments))};
m.prototype = o.prototype = m.extend({__array__:true, __intialize__:(intl||function(){}), length:0, selector:null, context:null, splice:[].splice}, (prot||{}));
m.extend(m,(ext||{}));
return m;
})( _api, _prototype, _extension, _prototype.go );
})(this);
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>JS.Custom Application Programming Interface</title>
</head>
<body>
<div>
<!-- Begin Body - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<h1>JS.Custom Application Programming Interface</h1>
<p id="testing">
This bit of JavaScript lets you implement your own custom api. Make your own...
</p>
<!-- End Body - - - - - - - - - - - - - - - - - - - - - - - - - - -->
</div>
<script src="customAPI.js"></script>
<script>
(function(window, undefined){
yourapinamehere(document.getElementById('testing')).helloWorld('context, wooot!!!');
yourapinamehere.helloWorldNoContext('no context, yeahhh!!!');
})(this);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment