Orphan is a pure javascript browser-based client for consuming RESTful services.
git clone https://github.com/julianbrowne/Orphan.git
See examples directory for list of ways Orphan can be used.
Include require.js and the main loader file:
<script src="/lib/require.js"></script>
<script src="/lib/loader.js"></script>
Create a client instance that points to a root location:
var client = new Orphan('http://localhost/');
Now call a resource on the server:
client.get('/some/resource/location', function(content) {
// do something with content here
});
What makes Orphan different from most other XHR wrap-around libraries (or just using $.get
in JQuery) is the suite of functions it contains to handle returned content.
For example:
function(content) {
var html = Orphan.markdown.makeHtml(content);
}
function(content) {
var obj = JSON.parse(content);
var html = Orphan.utils.inspectToHTML(obj);
}
function(content) {
var obj = JSON.parse(content);
var template = Orphan.template.ejs.create(path_to_ejs_template);
var html = template.render(obj);
}
function(content) {
var obj = JSON.parse(content);
var template = Orphan.template.mustache.create(path_to_mustache_template);
var html = template.render(obj);
}
The dynamic template engine renders JSON as HTML but converts embedded links into clickable hrefs so that they can be followed:
// create client object
var client = new Orphan("http://127.0.0.1/docs/");
// parse query string and grab any resource request (res=path/to/json) from the url
// if there is none then set a 'home' json resource
var qs = Orphan.utils.queryStringObj(location.href);
var resource = (qs['res']) ? qs['res'] : 'index.json';
client.get(resource, function(json) {
var obj = JSON.parse(json);
var template = Orphan.template.dynamic.create(cfg);
document.body.innerHTML = template.render(obj);
}
Any links: []
sections containing href
and rel
fields will be rendered as clickable links of the form <a href="http://host/docs/?res=path/to/json"
and will reload in the current page if clicked. Field names (links
, href
and rel
are configurable for any of the various standard specs).
Because Orphan is a client-side script it is subject to cross-site-scripting (XSS) rules and so source sites must either be 'local' (same protocol, hostname and port as Orphan) or must use CORS. Orphan also supports JSONP.