This is a simple demonstration of integrating a GI user interface with Reverse Ajax.
The stock data above is totally fictitious and delayed by 15 minutes.
The HTML includes a div which loads a GI component
<div style="width:100%; height:220px;">
<script
type="text/javascript"
src="JSX/js/JSX30.js"
jsxapppath="gidemo" jsxlt="true"> </script>
</div>
This causes GI to render the Matrix component from config.xml and the component declaration: appCanvas.xml. Once these are loaded the init function is called by GI.
function giLoaded() {
OpenAjax.subscribe("gidemo", "corporation", objectPublished);
dwr.engine.setActiveReverseAjax(true);
}
This subscribes to the OpenAjax hub listening for publications to the 'gidemo' + 'corporation' topic. When a publish happens, the objectPublished function is called. It also turns Reverse Ajax on. The objectPublished function looks like this:
function objectPublished(prefix, name, handlerData, corporation) {
var matrix = giApp.getJSXByName("matrix");
var inserted = matrix.getRecordNode(corporation.jsxid);
matrix.insertRecord(corporation, null, inserted == null);
matrix.repaintData();
}
This simply takes the published data and inserts it into the matrix component. That's it.
There are a number of possible repaint strategies, including the simple
matrix.repaintData(); documented above. The full source
contains a strategy that enables cell highlighting
Meanwhile, on the server the following code is running in a thread
while (!Thread.currentThread().isInterrupted())
{
Collection sessions = serverContext.getScriptSessionsByPage("/dwr/gi/index.html");
ScriptProxy proxy = new ScriptProxy(sessions);
Corporation corp = corporations.getNextChangedCorporation();
proxy.addFunctionCall("OpenAjax.publish", "gidemo", "corporation", corp);
int timeToSleep = random.nextInt(2500);
Thread.sleep(timeToSleep);
}
This simply finds the people viewing this page, and creates a
ScriptProxy to allow us to push Javascript to these users. We then ask
the corporations object for a Stock price change, and publish
this to the Open Ajax Hub. The hub has not been finally released yet, but
we are including a beta with this demo.
For the full source to all the files, including the config.xml and appCanvas.xml see the source in the war file.