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.
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
(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')); | |
} |
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. š