pgf_online.js: error callbacks

To enable customized error handling, the methods in the pgf_online objects and
the AJAX server call functions in support.js accept an error callback function
as an extra argument, in addition to the callback (continuation) for normal
results.
This commit is contained in:
hallgren
2012-03-22 14:09:53 +00:00
parent 604b1634b4
commit f8ea4b8046
2 changed files with 57 additions and 29 deletions

View File

@@ -5,48 +5,74 @@ function pgf_online(options) {
var server = {
// State variables (private):
grammars_url: "/grammars/",
other_grammars_urls: [],
grammar_list: null,
current_grammar_url: null,
// Methods:
switch_grammar: function(grammar_url,cont) {
this.current_grammar_url=this.grammars_url+grammar_url;
if(cont) cont();
},
get_grammarlist: function(cont) {
http_get_json(this.grammars_url+"grammars.cgi",cont);
add_grammars_url: function(grammars_url,cont) {
this.other_grammars_urls.push(grammars_url);
if(cont) cont();
},
pgf_call: function(cmd,args,cont) {
switch_to_other_grammar: function(grammar_url,cont) {
this.current_grammar_url=grammar_url;
if(cont) cont();
},
get_grammarlist: function(cont,err) {
if(this.grammar_list) cont(this.grammar_list)
else http_get_json(this.grammars_url+"grammars.cgi",cont,err);
},
get_grammarlists: function(cont,err) { // May call cont several times!
function pair(dir) {
return function(grammar_list){cont(dir,grammar_list)}
}
function ignore_error(err) { console.log(err) }
this.get_grammarlist(pair(this.grammars_url),err)
var ds=this.other_grammars_urls;
for(var i in ds)
http_get_json(ds[i]+"grammars.cgi",pair(ds[i]),ignore_error);
},
pgf_call: function(cmd,args,cont,err) {
var url=this.current_grammar_url+"?command="+cmd+encodeArgs(args)
http_get_json(url,cont);
http_get_json(url,cont,err);
},
get_languages: function(cont) { this.pgf_call("grammar",{},cont); },
grammar_info: function(cont) { this.pgf_call("grammar",{},cont); },
get_languages: function(cont,err) {
this.pgf_call("grammar",{},cont,err);
},
grammar_info: function(cont,err) {
this.pgf_call("grammar",{},cont,err);
},
get_random: function(args,cont) { // cat, limit
get_random: function(args,cont,err) { // cat, limit
args.random=Math.random(); // side effect!!
this.pgf_call("random",args,cont);
this.pgf_call("random",args,cont,err);
},
linearize: function(args,cont) { // tree, to
this.pgf_call("linearize",args,cont);
linearize: function(args,cont,err) { // tree, to
this.pgf_call("linearize",args,cont,err);
},
complete: function(args,cont) { // from, input, cat, limit
this.pgf_call("complete",args,cont);
complete: function(args,cont,err) { // from, input, cat, limit
this.pgf_call("complete",args,cont,err);
},
parse: function(args,cont) { // from, input, cat
this.pgf_call("parse",args,cont);
parse: function(args,cont,err) { // from, input, cat
this.pgf_call("parse",args,cont,err);
},
translate: function(args,cont) { // from, input, cat, to
this.pgf_call("translate",args,cont);
translate: function(args,cont,err) { // from, input, cat, to
this.pgf_call("translate",args,cont,err);
},
translategroup: function(args,cont) { // from, input, cat, to
this.pgf_call("translategroup",args,cont);
translategroup: function(args,cont,err) { // from, input, cat, to
this.pgf_call("translategroup",args,cont,err);
},
browse: function(id,cont,err) {
this.pgf_call("browse",{id:id,format:"json"},cont,err);
}
};
for(var o in options) server[o]=options[o];
if(server.grammar_list && server.grammar_list.length>0)
server.switch_grammar(server.grammar_list[0]);
return server;
}
}

View File

@@ -38,7 +38,7 @@ var json = {next:0};
// Like jsonp, but instead of passing the name of the callback function, you
// pass the callback function directly, making it possible to use anonymous
// functions.
function jsonpf(url,callback)
function jsonpf(url,callback,errorcallback)
{
var name="callback"+(json.next++);
json[name]=function(x) { delete json[name]; callback(x); }
@@ -71,10 +71,12 @@ function ajax_http(method,url,body,callback,errorcallback) {
else alert(errortext)
}
else {
var statechange=function() {
function statechange() {
if (http.readyState==4 || http.readyState=="complete") {
if(http.status<300) callback(http.responseText,http.status);
else if(errorcallback) errorcallback(http.responseText,http.status);
else if(errorcallback)
errorcallback(http.responseText,http.status,
http.getResponseHeader("Content-Type"));
else alert("Request for "+url+" failed: "
+http.status+" "+http.statusText);
}
@@ -96,8 +98,8 @@ function ajax_http_post(url,formdata,callback,errorcallback) {
}
// JSON via AJAX
function ajax_http_get_json(url,cont) {
ajax_http_get(url,function(txt) { cont(eval("("+txt+")")); });
function ajax_http_get_json(url,cont,errorcallback) {
ajax_http_get(url,function(txt){cont(eval("("+txt+")"));}, errorcallback);
}
function sameOrigin(url) {
@@ -107,9 +109,9 @@ function sameOrigin(url) {
}
// Use AJAX when possible, fallback to JSONP
function http_get_json(url,cont) {
if(sameOrigin(url)) ajax_http_get_json(url,cont);
else jsonpf(url,cont);
function http_get_json(url,cont,errorcallback) {
if(sameOrigin(url)) ajax_http_get_json(url,cont,errorcallback);
else jsonpf(url,cont,errorcallback);
}
/* --- URL construction ----------------------------------------------------- */