Load External JavaScript Files in on{X}


The other day I talked about the things I was playing with in on{X}. I like to use other frameworks or JavaScript libraries when I write my codez. Of course I’m going to want to do the same when writing rules in on{X}. This example shows how to load underscore.js for use in your on{X} rules.

(function() {
device.ajax(
{
url: 'http://underscorejs.org/underscore-min.js',
type: 'GET',
headers: {
'Content-Type': 'text/javascript'
}
},
function onSuccess(body, textStatus, response) {
eval(body);
if (_) console.log('underscore.js loaded successfully');
yourCode(_);
},
function onError(textStatus, response){
console.error('error code: ', response.status);
});
})();
function yourCode(_) {
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
console.info(_.pluck(stooges, 'name'));
}
view raw loadUnderscore.js hosted with ā¤ by GitHub

This should work with most libraries that you can access anonymously and via HTTP.

Disclaimer: Use at your own risk, I’m still just learning how this all works. šŸ™‚

← Hack Your Life with On{X}