Javascript API
Japano offers two features which normally require client-side javascript to work:-
Javascript views
Javascript views are a javascript view on a server-side object. A japano action is invoked and returns a javascript object which can be used to dynamically update the page - Partial page updates Parts of a JSP page can be marked so that they can be reloaded or loaded into other pages without doing a full update of the current HTML page in question.
Using the Javascrip API
To use the japano javascript API you must include the "japano.js" file
included in the japano distribution and instantiate a japano object by using
the following code:
The Japano object offers the following methods:var japano=new Japano();
- japano.get(URL, fn, params, waitCursor)
- Calls the given URL with the given associative parameter array params and feeds the evaluated JSON response to the function fn. If waitCursor is set to true, a wait cursor is set until a response is received.
- japano.getText(URL, fn, params, waitCursor)
- Calls the given URL with the given associative parameter array params and feeds the text response to the function fn. If waitCursor is set to true, a wait cursor is set until a response is received.
- japano.getHTML(URL, fn, params, waitCursor)
- Calls the given URL with the given associative parameter array params and feeds the received HTML element to the function fn. If waitCursor is set to true, a wait cursor is set until a response is received.
- japano.getXML(URL, fn, params, waitCursor)
- Calls the given URL with the given associative parameter array params and feeds the XML response to the function fn. If waitCursor is set to true, a wait cursor is set until a response is received.
- japano.convertToURL(URI)
- Converts the given URI to an URL.
- japano.readParameter(formElement)
- Reads the content of the given form element into an associative array.
- japano.commit(id, fn)
- Commits the form with the given id and reloads it via partial Update.
- japano.isError(o)
- Returns true if the given object is a japano Error object.
- japano.dump(o, fn)
- Dumps the given Object as HTML.
- japano.forSpan( className, fn)
-
Calls the given function for every span tag whose className contains the given className. the function is called with two arguments:
function fn(element, data) :- element
- the span DOM element.
- data
- data object embedded in the span.
The japano framework uses forSpan to create the buttons of <jpn:button> tags. The button tag inserts something like this into the page.
Japano calls forSpan() like this:<span class="jpn_button" onclick="data={button:'Push me!',fn:'doStuff'};">non js code</span>
forSpan() searches the DOM for spans containing "jpn_button" in their class attributes. Then it calls the function with the span element and the evaluated data object. The function creates the button with the given text that calls the given function when pushed. Without javascript only an empty span is there. fallback code can be added in <noscript></noscript> after that.this.forSpan("jpn_button", function(elem, data) { if (data) { var code="var fn="+data.fn+";"; eval(code); var button=document.createElement("BUTTON"); button.addEventListener("click", fn, false); while (elem.hasChildNodes()) elem.removeChild(elem.firstChild); elem.appendChild(button); button.appendChild(document.createTextNode(data.button)); } });
- japano.Error(code, response)
- This Object is thrown by japano to indicate an error.
