angular.js/src/Server.js

70 lines
1.9 KiB
JavaScript
Raw Normal View History

2010-01-12 01:32:33 +00:00
function Server(url, getScript) {
2010-01-06 00:36:58 +00:00
this.url = url;
this.nextId = 0;
this.getScript = getScript;
this.uuid = "_" + ("" + Math.random()).substr(2) + "_";
this.maxSize = 1800;
};
2010-01-12 01:32:33 +00:00
Server.prototype = {
base64url: function(txt) {
return Base64.encode(txt);
},
request: function(method, url, request, callback) {
var requestId = this.uuid + (this.nextId++);
angularCallbacks[requestId] = function(response) {
delete angular[requestId];
callback(200, response);
};
var payload = {u:url, m:method, p:request};
payload = this.base64url(toJson(payload));
var totalPockets = Math.ceil(payload.length / this.maxSize);
var baseUrl = this.url + "/$/" + requestId + "/" + totalPockets + "/";
for ( var pocketNo = 0; pocketNo < totalPockets; pocketNo++) {
var pocket = payload.substr(pocketNo * this.maxSize, this.maxSize);
this.getScript(baseUrl + (pocketNo+1) + "?h=" + pocket, noop);
}
2010-01-06 00:36:58 +00:00
}
};
2010-01-12 01:32:33 +00:00
function FrameServer(frame) {
2010-01-06 00:36:58 +00:00
this.frame = frame;
};
2010-01-09 23:02:43 +00:00
FrameServer.PREFIX = "$DATASET:";
2010-01-06 00:36:58 +00:00
2010-01-09 23:02:43 +00:00
FrameServer.prototype = {
2010-01-06 00:36:58 +00:00
read:function(){
2010-01-09 23:02:43 +00:00
this.data = fromJson(this.frame.name.substr(FrameServer.PREFIX.length));
2010-01-06 00:36:58 +00:00
},
write:function(){
2010-01-09 23:02:43 +00:00
this.frame.name = FrameServer.PREFIX + toJson(this.data);
2010-01-06 00:36:58 +00:00
},
request: function(method, url, request, callback) {
2010-01-09 23:02:43 +00:00
//alert(method + " " + url + " " + toJson(request) + " " + toJson(callback));
2010-01-06 00:36:58 +00:00
}
};
2010-01-12 01:32:33 +00:00
function VisualServer(delegate, status, update) {
2010-01-06 00:36:58 +00:00
this.delegate = delegate;
this.update = update;
this.status = status;
};
2010-01-09 23:02:43 +00:00
VisualServer.prototype = {
2010-01-06 00:36:58 +00:00
request:function(method, url, request, callback) {
var self = this;
this.status.beginRequest(request);
this.delegate.request(method, url, request, function() {
self.status.endRequest();
try {
callback.apply(this, arguments);
} catch (e) {
2010-01-09 23:02:43 +00:00
alert(toJson(e));
2010-01-06 00:36:58 +00:00
}
self.update();
});
}
};