AjaxStub

The AjaxStub class is written using closures and auto instantiates when ajaxstub.js file is fully loaded. The instantiated object is always AjaxStub which is also used to call the constructor of the class-yes after it was instantiated. Douglas Crockford http://www.crockford.com/javascript/private.html is credited for developing this methodology.

Constructor
The constructor takes three arguments; url is set by the developers application; contentType and funcList are set by the Python AjaxStub API.
var contentType = "text/x-json";

var funcList = ['exception', 'logging', 'username',

                'getExamples', 'login', 'submit'];


window.onload = function() {

  AjaxStub("http://yoururl.com/yourscript.py",

           contentType, funcList);

}


In the example above url is coded by the developer and the last two arguments contentType and funcList are pushed to the web page by the Python AjaxStub API.

log(value)
Will log messages through a callback function named loggingCB if you have it defined. You will also need to define a variable named debug and set it to true. Note: Do not use log if you have not defined loggingCB.
var debug = true;


var loggingCB = function() {

  var logging = document.getElementById("logging");

  logging.innerHTML += result + "<br />";

  logging.scrollTop = 1000000;

}


var someFunction = function() {

  ...

  AjaxStub.log('Results of some process.');

  ...

}


In the above example first set a variable debug to true then write a callback that does something with your log message so when you use log in your code the message has someplace to go.

register(list, obj)
This can be used to register stubs, but the stubs must have already been defined by the Python API. The list argument contains the stubs you wish to register and obj is the object they are in ie. class. The register function defaults to the window scope to look for callbacks, so the obj argument is rarely needed.

It might seem confusing as to why, if you registered your stubs in the Python API, you would need to do it again. Well you don't unless you push JavaScript code up to your page which you may want to delete later. The register method is called in the constructor and through introspection finds the stubs and the callbacks. When the class first gets instantiated it will not find the callbacks which you have just pushed up to your page. You must write and pre-register the stubs, however; because the Python API will at the time the page is requested create and put them in the page. I suppose if you wanted to write the stubs yourself and push them up also this may work, but I have never tried this approach myself.

var list = ['foo', 'bar'];

AjaxStub.register(list);

unregister(list)
This is used to unregister stubs or stub callback.
var list = ['foo', 'bar'];

AjaxStub.unregister(list);

getStubFunction(name)
Returns the object of a registered stub or stub callback.
var funcObj = AjaxStub.getStubFunction('foo');
executeStub(name, cgiMethod, args)
This method is generally used in the stubs pushed up to your page by the Python API. It can be used if you push up your own stubs in an AJAX request to the server. The name argument is the function or method name in your Python code, cgiMethod is either 'GET' or 'POST', and args is the argument list of the JavaScript stub calling this method.
// Stub for function: foo()

var foo = function() {

  AjaxStub.executeStub('foo', 'GET', foo.arguments);

}


The code above is generally auto generated by the Python AjaxStub API.

getNodeText(doc)
This is a convenience method that gets the text in an XML tag. It will loop through all the nodes in a tag looking for text and cdata nodes and concatenating them together forming the full text of the tag. The doc argument is the tag object that you want the text from.

Carl J. Nobile 2007-08-02