Created
October 17, 2012 13:05
-
-
Save christophergregory/3905403 to your computer and use it in GitHub Desktop.
JavaScript: OpenKeyVal Wrapper
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
/* | |
OpenKeyVal Wrapper | |
Author: Chris Gregory ([email protected]) | |
Description: Obscures openkeyvalue variable names for added protection against variable name overwrites | |
*/ | |
var OKV = (function($, sudoKey) { | |
var okv = {}, | |
sudoAPIkey = sudoKey || "KqTZsiBoeZMnERP"; // Default sudoKey | |
// Obscures keys with a sudoKey to prevent overwrites | |
function prefix(key) { | |
return sudoAPIkey + "-" + key; | |
} | |
okv.setItem = function(key, value, callback) { | |
var args = arguments.length; | |
$.ajax({ | |
url: "https://secure.openkeyval.org/store/", | |
data: prefix(key) + "=" + value, | |
dataType: "jsonp", | |
success: function(data){ | |
if (args === 3) { | |
callback(key, value); | |
} else { | |
console.log( key + " - " + value ); | |
} | |
} | |
}); | |
} | |
okv.getItem = function(key, callback) { | |
var args = arguments.length; | |
$.ajax({ | |
url: "https://secure.openkeyval.org/" + prefix(key), | |
dataType: "jsonp", | |
success: function(data){ | |
if (args === 2) { | |
callback(data, key); | |
} else { | |
console.log( key + " - " + data ); | |
} | |
} | |
}); | |
} | |
return okv; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment