BackendService.lzx
<library>
<class name="BackendService" extends="node">
<attribute name="successDatapath" type="string" value="
"/>
<attribute name="failureDatapath" type="string" value="
"/>
<attribute name="trigger" type="number" value="0
"/>
<attribute name="clearMessageDel" type="expression" value="null
"/>
<attribute name="clearDelay" type="number" value="2500
"/>
<attribute name="httpMethod" type="string" value="GET
"/>
<attribute name="_connector" type="expression" value="null
"/>
<attribute name="_dsRef" type="expression" value="null
"/>
<attribute name="_errorCount" type="number" value="0
"/>
<attribute name="_lastMessage" type="string" value="
"/>
<attribute name="_lastRequestTime" type="expression" value="null
"/>
<attribute name="_requestCount" type="number" value="0
"/>
<attribute name="_xmlSerialized" type="string" value="no XML document received yet
"/>
<attribute name="IDLE" type="string" value="idle
"/>
<attribute name="REQUEST_STARTED" type="string" value="request started
"/>
<attribute name="PROCESSING_RESPONSE" type="string" value="processing response
"/>
<attribute name="WAITING_FOR_CLEAR" type="string" value="idle
"/>
<datapointer name="_successDP" rerunxpath="true">
<handler name="ondata" args="p">
if (p !=null) {
var dupe = this.dupePointer();
dupe.setXPath("/");
classroot._xmlSerialized = dupe.serialize();
classroot._lastMessage = this.getNodeText();
classroot.handleSuccess(this.getNodeText(), p);
classroot.setupTimer();
classroot.ondata.sendEvent();
}
</handler>
<handler name="onerror" args="err">
classroot._errorCount++;
classroot.handleError();
</handler>
<handler name="ontimeout" args="e">
Debug.error(e);
Debug.error('Timeout for REST webservice ' + this.name);
</handler>
</datapointer>
<datapointer name="_failureDP" rerunxpath="true">
<handler name="ondata" args="p">
if (p !=null) {
var dupe = this.dupePointer();
dupe.setXPath("/");
classroot._xmlSerialized = dupe.serialize();
classroot._lastMessage = this.getNodeText();
classroot.handleFailure(this.getNodeText());
classroot.setupTimer();
classroot.ondata.sendEvent();
}
</handler>
</datapointer>
<event name="ontrigger"/>
<event name="ondata"/>
<handler name="onconstruct">
if (typeof(canvas.services) == 'undefined') {
canvas.services = new Array();
}
</handler>
<method name="setupTimer">
if (typeof this.clearMessage == 'function') {
if (this.clearMessageDel == null) {
this.clearMessageDel = new LzDelegate( this, "clearMessage" );
} else {
this.clearMessageDel.unregisterAll();
}
lz.Timer.addTimer( this.clearMessageDel, this.clearDelay);
}
</method>
<method name="init">
super.init();
_successDP.setAttribute('xpath', this.successDatapath);
// While the succuss datapath is is a requirement, the
// failure datapath is optional.
if (this.failureDatapath != null) {
_failureDP.setAttribute('xpath', this.failureDatapath);
}
this._dsRef = canvas[this.name+'DS'];
// Push this service object to the canvas.services array
canvas.services[this.name] = this;
</method>
<method name="prepareParams" args="values">
for (var key in values) {
Debug.write('Adding var ' + key + '=' + values[key] + ' to params');
this._dsRef.setQueryParam(key, values[key]);
}
this.startRequest();
</method>
<method name="handleError">
Debug.error("Error calling REST web service " + this.name);
</method>
<method name="startRequest">
this._lastRequestTime = new Date();
this._requestCount++;
this._dsRef.setAttribute('querytype', this.httpMethod);
this._dsRef.doRequest();
</method>
</class>
</library>