Simplify Javascript component reference API a bit. Rename tuscany-ref.js to ref.js. Add a function to declare reference proxy methods.

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1027835 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
jsdelfino 2010-10-27 06:57:31 +00:00
commit 0fa885699a
15 changed files with 159 additions and 204 deletions

View file

@ -19,10 +19,10 @@
<html>
<head>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Protected");
var userInfo = new tuscany.sca.Reference("userInfo");
var protected = component("Protected");
var userInfo = reference(protected, "userInfo");
var user = userInfo.apply("getuser");
var email = userInfo.apply("getemail");
var nickname = userInfo.apply("getnickname");

View file

@ -19,10 +19,10 @@
<html>
<head>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Protected");
var userInfo = new tuscany.sca.Reference("userInfo");
var protected = component("Protected");
var userInfo = reference(protected, "userInfo");
var user = userInfo.apply("getuser");
var email = userInfo.apply("getemail");
</script>

View file

@ -30,7 +30,9 @@
/**
* Escape a character.
*/
function escapeJSONChar(c) {
var JSONClient = new Object();
JSONClient.escapeJSONChar = function(c) {
if(c == "\"" || c == "\\") return "\\" + c;
else if (c == "\b") return "\\b";
else if (c == "\f") return "\\f";
@ -42,16 +44,16 @@ function escapeJSONChar(c) {
else if(hex.length == 2) return "\\u00" + hex;
else if(hex.length == 3) return "\\u0" + hex;
else return "\\u" + hex;
}
};
/**
* Encode a string into JSON format.
*/
function escapeJSONString(s) {
JSONClient.escapeJSONString = function(s) {
/* The following should suffice but Safari's regex is broken
(doesn't support callback substitutions)
return "\"" + s.replace(/([^\u0020-\u007f]|[\\\"])/g,
escapeJSONChar) + "\"";
JSONClient.escapeJSONChar) + "\"";
*/
/* Rather inefficient way to do it */
@ -62,19 +64,19 @@ function escapeJSONString(s) {
c == '\\' ||
c.charCodeAt(0) < 32 ||
c.charCodeAt(0) >= 128)
parts[i] = escapeJSONChar(parts[i]);
parts[i] = JSONClient.escapeJSONChar(parts[i]);
}
return "\"" + parts.join("") + "\"";
}
};
/**
* Marshall objects to JSON format.
*/
function toJSON(o) {
JSONClient.toJSON = function(o) {
if(o == null) {
return "null";
} else if(o.constructor == String) {
return escapeJSONString(o);
return JSONClient.escapeJSONString(o);
} else if(o.constructor == Number) {
return o.toString();
} else if(o.constructor == Boolean) {
@ -83,21 +85,21 @@ function toJSON(o) {
return '{javaClass: "java.util.Date", time: ' + o.valueOf() +'}';
} else if(o.constructor == Array) {
var v = [];
for(var i = 0; i < o.length; i++) v.push(toJSON(o[i]));
for(var i = 0; i < o.length; i++) v.push(JSONClient.toJSON(o[i]));
return "[" + v.join(", ") + "]";
} else {
var v = [];
for(attr in o) {
if(o[attr] == null) v.push("\"" + attr + "\": null");
else if(typeof o[attr] == "function"); /* skip */
else v.push(escapeJSONString(attr) + ": " + toJSON(o[attr]));
else v.push(JSONClient.escapeJSONString(attr) + ": " + JSONClient.toJSON(o[attr]));
}
return "{" + v.join(", ") + "}";
}
}
};
/**
* HTTPBindingClient.Exception
* HTTPBindingClient.Exception.
*/
HTTPBindingClient.Exception = function(code, message, javaStack) {
this.code = code;
@ -120,16 +122,16 @@ HTTPBindingClient.Exception.CODE_ERR_UNMARSHALL = 592;
HTTPBindingClient.Exception.CODE_ERR_MARSHALL = 593;
HTTPBindingClient.Exception.prototype = new Error();
HTTPBindingClient.Exception.prototype.toString = function(code, msg)
{
HTTPBindingClient.Exception.prototype.toString = function(code, msg) {
return this.name + ": " + this.message;
};
/**
* Default top level exception handler
* Default top level exception handler.
*/
HTTPBindingClient.default_ex_handler = function(e) { alert(e); };
HTTPBindingClient.default_ex_handler = function(e) {
alert(e);
};
/**
* Client settable variables
@ -139,7 +141,6 @@ HTTPBindingClient.profile_async = false;
HTTPBindingClient.max_req_active = 1;
HTTPBindingClient.requestId = 1;
/**
* HTTPBindingClient implementation
*/
@ -258,7 +259,7 @@ HTTPBindingClient.prototype._makeRequest = function(methodName, args, cb) {
if (cb) req.cb = cb;
if (HTTPBindingClient.profile_async)
req.profile = { "submit": new Date() };
req.data = toJSON(obj);
req.data = JSONClient.toJSON(obj);
return req;
};
@ -424,7 +425,7 @@ HTTPBindingClient.prototype.get = function(id, responseFunction) {
}
xhr.open("GET", this.uri + '/' + id, true);
xhr.send(null);
}
};
HTTPBindingClient.prototype.post = function (entry, responseFunction) {
var xhr = this.createXMLHttpRequest();
@ -445,7 +446,7 @@ HTTPBindingClient.prototype.post = function (entry, responseFunction) {
xhr.open("POST", this.uri, true);
xhr.setRequestHeader("Content-Type", "application/atom+xml");
xhr.send(entry);
}
};
HTTPBindingClient.prototype.put = function (id, entry, responseFunction) {
var xhr = this.createXMLHttpRequest();
@ -466,7 +467,7 @@ HTTPBindingClient.prototype.put = function (id, entry, responseFunction) {
xhr.open("PUT", this.uri + '/' + id, true);
xhr.setRequestHeader("Content-Type", "application/atom+xml");
xhr.send(entry);
}
};
HTTPBindingClient.prototype.del = function (id, responseFunction) {
var xhr = this.createXMLHttpRequest();
@ -481,7 +482,7 @@ HTTPBindingClient.prototype.del = function (id, responseFunction) {
}
xhr.open("DELETE", this.uri + '/' + id, true);
xhr.send(null);
}
};
HTTPBindingClient.prototype.createXMLHttpRequest = function () {
/* Mozilla XMLHttpRequest */
@ -495,25 +496,6 @@ HTTPBindingClient.prototype.createXMLHttpRequest = function () {
return null;
}
/**
* Create Tuscany namespace.
*/
var tuscany;
if (!tuscany)
tuscany = {};
if (!tuscany.sca)
tuscany.sca = {};
/**
* Configure component name
*/
tuscany.sca.componentName = "Default";
tuscany.sca.Component = function(name) {
tuscany.sca.componentName = name;
return name
}
/**
* Construct an HTTPBindingClient.
*/
@ -540,14 +522,53 @@ function HTTPBindingClient(cname, uri, objectID) {
req.send(null);
return req.responseXML;
}
}
}
}
}
};
/**
* Construct a reference proxy
* Construct a component.
*/
tuscany.sca.Reference = function(name) {
return new HTTPBindingClient(tuscany.sca.componentName, name);
function ClientComponent(name) {
this.name = name;
}
/**
* Public API.
*/
/**
* Return a component.
*/
function component(name) {
return new ClientComponent(name);
}
/**
* Return a reference proxy.
*/
function reference(comp, name) {
return new HTTPBindingClient(comp.name, name);
};
/**
* Add proxy functions to a reference proxy.
*/
function defun(ref) {
function defapply(name) {
return function() {
var args = new Array();
args[0] = name;
for (i = 0, n = arguments.length; i < n; i++)
args[i + 1] = arguments[i];
this.apply.apply(this, args);
};
}
for (f = 1; f < arguments.length; f++) {
var fn = arguments[f];
ref[fn]= defapply(fn);
}
return ref;
};

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = reference(store, "shoppingCart");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -64,7 +58,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -119,7 +113,7 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = reference(store, "shoppingCart");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -64,7 +58,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -119,7 +113,7 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = reference(store, "shoppingCart");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -64,7 +58,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -119,7 +113,7 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = defun(reference(store, "shoppingCart"), "email", "host");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -51,7 +45,7 @@ function catalog_itemsResponse(items, exception) {
catalogItems = items;
}
function shoppingCart_gethostResponse(host, exception) {
function shoppingCart_hostResponse(host, exception) {
if (exception) {
alert(exception.message);
return;
@ -59,7 +53,7 @@ function shoppingCart_gethostResponse(host, exception) {
document.getElementById('host').innerHTML = host;
}
function shoppingCart_getemailResponse(email, exception) {
function shoppingCart_emailResponse(email, exception) {
if (exception) {
alert(exception.message);
return;
@ -79,7 +73,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -136,9 +130,9 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
shoppingCart.apply("getemail", shoppingCart_getemailResponse);
shoppingCart.apply("gethost", shoppingCart_gethostResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.email(shoppingCart_emailResponse);
shoppingCart.host(shoppingCart_hostResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);

View file

@ -73,10 +73,10 @@ def total(cache, host, email):
return sum(cart)
# Return the email of the cart owner
def getemail(cache, host, email):
return email.eval()
def email(cache, host, email_):
return email_.eval()
# Return the host that the app is running on
def gethost(cache, host, email):
return host.eval()
def host(cache, host_, email):
return host_.eval()

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = reference(store, "shoppingCart");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -64,7 +58,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -119,7 +113,7 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = reference(store, "shoppingCart");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -64,7 +58,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -119,7 +113,7 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = reference(store, "shoppingCart");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -64,7 +58,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -119,7 +113,7 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = reference(store, "shoppingCart");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -64,7 +58,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -119,7 +113,7 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = reference(store, "shoppingCart");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -64,7 +58,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -119,7 +113,7 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = reference(store, "shoppingCart");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -64,7 +58,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -119,7 +113,7 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);

View file

@ -20,19 +20,13 @@
<head>
<title>Store</title>
<script type="text/javascript" src="/js/tuscany-ref.js"></script>
<script type="text/javascript" src="/js/ref.js"></script>
<script type="text/javascript">
var component = new tuscany.sca.Component("Store");
//@Reference
var catalog = new tuscany.sca.Reference("catalog");
//@Reference
var shoppingCart = new tuscany.sca.Reference("shoppingCart");
//@Reference
var shoppingTotal = new tuscany.sca.Reference("shoppingTotal");
var store = component("Store");
var catalog = defun(reference(store, "catalog"), "items");
var shoppingCart = reference(store, "shoppingCart");
var shoppingTotal = defun(reference(store, "shoppingTotal"), "total");
var catalogItems;
@ -64,7 +58,7 @@ function shoppingCart_getResponse(feed) {
}
document.getElementById("shoppingCart").innerHTML = list;
shoppingTotal.apply("total", shoppingTotal_totalResponse);
shoppingTotal.total(shoppingTotal_totalResponse);
}
}
@ -119,7 +113,7 @@ function deleteCart() {
function init() {
try {
catalog.apply("items", catalog_itemsResponse);
catalog.items(catalog_itemsResponse);
shoppingCart.get("", shoppingCart_getResponse);
} catch(e){
alert(e);