3 if (typeof(dojo) != 'undefined') {
4 dojo.provide("MochiKit.JsonRpc");
5 dojo.require("MochiKit.Base");
6 dojo.require("MochiKit.DOM");
7 dojo.require("MochiKit.Async");
10 if (typeof(JSAN) != 'undefined') {
11 JSAN.use("MochiKit.Base", []);
12 JSAN.use("MochiKit.DOM", []);
13 JSAN.use("MochiKit.Async", []);
17 if (typeof(MochiKit.Base) == 'undefined' ||
18 typeof(MochiKit.DOM) == 'undefined' ||
19 typeof(MochiKit.Async) == 'undefined') {
23 throw "MochiKit.JsonRpc depends on MochiKit.Base, MochiKit.DOM and MochiKit.Async";
26 if (typeof(MochiKit.JsonRpc) == 'undefined') {
27 MochiKit.JsonRpc = {};
30 MochiKit.JsonRpc.NAME = "MochiKit.JsonRpc";
31 MochiKit.JsonRpc.VERSION = "0.90";
33 MochiKit.JsonRpc.__repr__ = function () {
34 return "[" + this.NAME + " " + this.VERSION + "]";
37 MochiKit.JsonRpc.toString = function () {
38 return this.__repr__();
41 MochiKit.JsonRpc.JsonRpcError = function (message) {
42 this.message = message;
43 this.name = 'JsonRpcError';
46 MochiKit.JsonRpc.JsonRpcError.prototype = new Error();
47 MochiKit.JsonRpc.JsonRpcError.prototype.repr = function () {
48 return 'JsonRpcError(' + this.message + ')';
51 MochiKit.JsonRpc.JsonRpcError.prototype.toString = function () {
55 MochiKit.JsonRpc.jsonObject = function (o) {
58 if(typeof o[attr] != "function"){
59 attrs.push('"' + attr + '": ' + json(o[attr]));
62 return "{" + attrs.join(", ") + "}";
65 MochiKit.JsonRpc.isObject = function (o) {
69 MochiKit.JsonRpc.jsonArray = function (o) {
70 return "[" + MochiKit.Base.map(json, o).join(", ") + "]";
73 var MB = MochiKit.Base
75 MochiKit.JsonRpc.jsonRegistry = new MochiKit.Base.AdapterRegistry();
76 MochiKit.JsonRpc.jsonRegistry.register('arrayLike',MB.isArrayLike,MochiKit.JsonRpc.jsonArray);
77 MochiKit.JsonRpc.jsonRegistry.register("string", MB.typeMatcher("string"), MB.reprString);
78 MochiKit.JsonRpc.jsonRegistry.register("numbers", MB.typeMatcher("number", "boolean"), MB.reprNumber);
79 MochiKit.JsonRpc.jsonRegistry.register("undefined", MB.isUndefined, MB.reprUndefined);
80 MochiKit.JsonRpc.jsonRegistry.register("null", MB.isNull, MB.reprNull);
81 MochiKit.JsonRpc.jsonRegistry.register("objectLike", MochiKit.JsonRpc.isObject, MochiKit.JsonRpc.jsonObject);
83 MochiKit.JsonRpc.json = function (o) {
85 if (typeof(o.__json__) == 'function') {
87 } else if (typeof(o.json) == 'function' && o.json != arguments.callee) {
90 return jsonRegistry.match(o);
92 if (typeof(o.NAME) == 'string' && (
93 o.toString == Function.prototype.toString ||
94 o.toString == Object.prototype.toString
104 MochiKit.JsonRpc.JsonRpcCall = function (method,params) {
105 this.method = method;
106 this.params = params;
110 MochiKit.JsonRpc.JsonRpcProxy = function (url,methNames) {
111 MochiKit.Base.bindMethods(this);
114 MochiKit.Base.map(this._proxyMethod,methNames);
118 update(MochiKit.JsonRpc.JsonRpcProxy.prototype, {
119 'call': function () {
120 var arglist = MochiKit.Base.map(null,arguments)
121 var methname = arglist.shift()
123 var callobj = new MochiKit.JsonRpc.JsonRpcCall(methname,arglist);
124 var callstr = json(callobj);
125 var req = MochiKit.Async.getXMLHttpRequest();
126 req.open("POST",this.url,true);
127 req.setRequestHeader("Content-Type","text/plain");
128 req.setRequestHeader("Content-Length",callstr.length);
129 var d = MochiKit.Async.sendXMLHttpRequest(req,callstr);
130 d.addCallback(MochiKit.Async.evalJSONRequest);
131 d.addCallback(this._extractResult);
135 'addSingleMethod': function (methName) {
137 this._proxyMethod(methName);
140 'addMethods': function (methNames) {
142 MochiKit.Base.map(this._proxyMethod,methNames);
145 '_extractResult': function (resp) {
149 throw new MochiKit.JsonRpc.JsonRpcError(resp.error);
152 '_proxyMethod': function (methname) {
153 this[methname] = MochiKit.Base.partial(this.call,methname);
157 MochiKit.JsonRpc.DomObjectFromJson = function (){
159 if (arguments.length == 1) {
160 var arg = arguments[0];
161 if (typeof(arg) == 'string'){
162 retval = MochiKit.DOM.SPAN(null,arg);
164 var objrepr = arguments[0];
165 var elem = document.createElement(objrepr[0]);
166 var attrs = objrepr[1];
168 MochiKit.DOM.updateNodeAttributes(elem, attrs);
170 if (objrepr.length >= 3){
171 var extraobj = objrepr[2]
172 for (var i=0;i<extraobj.length;i++) {
173 var value = MochiKit.JsonRpc.DomObjectFromJson(extraobj[i]);
175 elem.appendChild(value);
185 MochiKit.JsonRpc.EXPORT = [
190 MochiKit.JsonRpc.EXPORT_OK = [
199 MochiKit.JsonRpc.__new__ = function () {
202 ":common": this.EXPORT,
203 ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK)
206 MochiKit.Base.nameFunctions(this);
210 MochiKit.JsonRpc.__new__();
212 if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
213 || (typeof(MochiKit.__compat__) == 'boolean' && MochiKit.__compat__)) {
215 var all = self.EXPORT_TAGS[":all"];
216 for (var i = 0; i < all.length; i++) {
217 this[all[i]] = self[all[i]];
219 })(MochiKit.JsonRpc);