/*
-
Lightweight JSONP fetcher
-
Copyright 2010-2012 Erik Karlsson. All rights reserved.
-
BSD licensed
*/
/*
-
Usage:
*
-
JSONP.get( 'someUrl.php', {param1:'123', param2:'456'}, function(data){
-
//do something with data, which is the JSON object you should retrieve from someUrl.php
-
});
*/ var JSONP = function(){
var counter = 0, head, config = {}; function load(url, pfnError) { var script, done, errorHandler; script = document.createElement('script'); done = false; script.src = url; script.async = true; errorHandler = pfnError || config.error; if ( typeof errorHandler === 'function' ) { script.onerror = function(ex){ errorHandler({url: url, event: ex}); }; } script.onload = script.onreadystatechange = function() { if ( !done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") ) { done = true; script.onload = script.onreadystatechange = null; if ( script && script.parentNode ) { script.parentNode.removeChild( script ); } } }; if ( !head ) { head = document.getElementsByTagName('head')[0]; } head.appendChild( script ); } function encode(str) { return encodeURIComponent(str); } function jsonp(url, params, callback, callbackName) { var query = (url||'').indexOf('?') === -1 ? '?' : '&', key, uniqueName; callbackName = (callbackName||config['callbackName']||'callback'); uniqueName = callbackName + "_json" + (++counter); params = params || {}; for ( key in params ) { if ( params.hasOwnProperty(key) ) { query += encode(key) + "=" + encode(params[key]) + "&"; } } window[ uniqueName ] = function(data){ callback(data); try { delete window[ uniqueName ]; } catch (e) {} window[ uniqueName ] = null; }; load(url + query + callbackName + '=' + uniqueName); return uniqueName; } function setDefaults(obj){ config = obj; } return { get:jsonp, init:setDefaults };
};
/*global JSONP */ if (window.Transistor === undefined) {
window.Transistor = {};
}
(function (transistor) {
var Control = (function () { return function (broadcaster, station_uid, token) { var jsonp, set, insert, update, remove, command_url, send; jsonp = JSONP(); set = function (channel, collection, cb) { send(command_url('set', channel), {collection: collection}, cb); }; insert = function (channel, entry, cb) { send(command_url('insert', channel), {entry: entry}, cb); }; update = function (channel, id, entry, cb) { send(command_url('update', channel), {id: id, entry: entry}, cb); }; remove = function (channel, id, cb) { send(command_url('remove', channel), {id: id}, cb); }; command_url = function (cmd, channel) { var base = broadcaster + '/command/' + cmd + '/' + station_uid + '/' + token; if (channel === undefined) { return base; } else { return base + '/' + channel; } }; send = function (command_url, data, cb) { if (cb === undefined) { cb = function (result) {}; } if (data === undefined) { jsonp.get(command_url, {}, cb); } else { jsonp.get(command_url, {data: JSON.stringify(data)}, cb); } }; this.set = set; this.insert = insert; this.update = update; this.remove = remove; }; }()); transistor.Control = Control;
}(window.Transistor));