Version 9, last updated by slim at Mar 19 03:36 2007 UTC
JsonCallbackInfotron
This is another is_proxy wrapper which doesn't use input terminals, but traps acess to the wrapped/embedded achor element.
Demos that use it
- 43.html
- flickr_and_webshot.html
- evdb.html
- rss_feeds.html
- rss_feeds_timeline.html
- yahoosearch.html
- map.html
- multi_map.html
BLUEPRINT(
"~011A83EC1CBFAD4a3188344C9BA816DE73",
[
[null, null, -1] // magic
],
[
"response_out"
],
function (Class) {
Class.prototype._onInit = function(props)
{
var a = this.dom_node.getElementsByTagName("A")[0];
//#ifdef __DEBUG__
if (!a || a.rel != "rpc" || !a.href || !a.getAttribute("impl")) {
throw "This is a proxy infotron: an anchor tag of the format \
<a rel=\"rpc\" href=\"URI\" impl=\"UUID\"></a> is required!";
/***********************************************************************/
}
if (this.dom_node.getAttribute("is_proxy") != "true") {
throw "This is a proxy infotron: you must specify is_proxy=\"true\"";
/***********************************************************************/
}
//#endif
/*************************************************************************/
this._base_url = a.href || "/";
this._uri_suffix = props["uri_suffix"] || "";
this._path_suffix = props["path_suffix"] || props["url_suffix"] || "";
this._encoding = props["encoding"] || "json";
this._cb_func_name = props["callback_function"] || "callback";
this._use_plus = props["escapes_space_with_plus_sign"];
this._add_random_qs = props["adds_randomized_query"];
};
Class.prototype._onMsgDelivery = function(iterm, msg)
{
var o, e;
if (iterm != "%startup") {
var type;
var url = this._createUrl(iterm, msg);
o =__d.createElement("SCRIPT");
o.type = "text/javascript";
o.src = url;
this.debug(url);
this.dom_node.appendChild(o);
}
};
Class.prototype._onResponse = function(msg)
{
this.postMessage("response_out", msg);
}
/*************************************************************************/
Class.prototype._createUrl = function(iterm, msg)
{
var args, v;
var random_qs = "";
if (this._encoding == "uri-form") {
args = ""
for (key in msg) {
v = escape(msg[key]);
if (this._use_plus) {
try {
v = v.replace(/%20/g, '%2b');
} catch (ex) {
}
}
args += escape(key) + "=" + v + "&";
}
} else {
args = "msg=" + escape(this._toJson(msg)) + "&";
}
if (this._add_random_qs) {
random_qs = "__r=" + (new Date()).valueOf() + "_" + Math.random() + "&";
}
return this._base_url + "/" + escape(iterm) + this._path_suffix + "?" +
this._cb_func_name + "=__star.infotrons." +
this.id + "._onResponse" + "&" + random_qs + args + this._uri_suffix;
}
Class.prototype._toJson = function(obj)
{
// modified from http://www.crockford.com/JSON/js.html
// being used for GOOD not evil!!
var c, i, l, s = '', v;
switch (typeof(obj)) {
case 'object':
if (obj) {
if (obj.constructor == Array) {
for (i = 0; i < obj.length; ++i) {
v = this._toJson(obj[i]);
if (s) {
s += ',';
}
s += v;
}
return '[' + s + ']';
} else if (obj.constructor == Date) {
return obj.valueOf();
} else if (typeof obj.toString != 'undefined') {
for (i in obj) {
v = obj[i];
if (typeof v != 'undefined' && typeof v != 'function') {
v = this._toJson(v);
if (s) {
s += ',';
}
s += this._toJson(i) + ':' + v;
}
}
return '{' + s + '}';
}
}
return 'null';
case 'number':
return isFinite(obj) ? (new String(obj)) : 'null';
case 'string':
l = obj.length;
s = '"';
for (i = 0; i < l; i += 1) {
c = obj.charAt(i);
if (c >= ' ') {
if (c == '\\' || c == '"') {
s += '\\';
}
s += c;
} else {
switch (c) {
case '\b':
s += '\\b';
break;
case '\f':
s += '\\f';
break;
case '\n':
s += '\\n';
break;
case '\r':
s += '\\r';
break;
case '\t':
s += '\\t';
break;
default:
c = c.charCodeAt();
s += '\\u00' + Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}
}
}
return s + '"';
case 'boolean':
return (new String(obj));
default:
return 'null';
}
}
}, "JSON Callback Service Proxy");