From 2009.igem.org
/*
Copyright: SynBioWave
License: GPL
Authors: David Nellessen (mail@davidn.de)
DESCRIPTION: This class qooxwaveclient.Store is the bridge between the actual data stored
(in the wave) and the model which is used to build the UI (and interact with wave).
You need to pass the wave object, the observed State Keys and the state key, that points to the
model data. When using this class, you should also set the wave statecallback to processStateUpdate.
you can use qooxdoos powerfull bind feature to bind the applications model object to the model
property of this object. So it will keep in sync the data!
TODO:
*/
qx.Class.define("qooxwaveclient.Store",
{
extend : qx.core.Object,
//stateKeys must be an array of stateKeys controlled by this store object
construct : function(wave, observedStateKeys, modelStateKey)
{
this.base(arguments);
this.__wave = wave;
this.__state = wave.getState();
this.__stateKeys = observedStateKeys;
this.__modelKey = modelStateKey;
//this.json = new qx.data.marshal.Json();
},
properties : {
connectionsIn : {check : "qx.data.Array", event: "ChangeConnectionsIn", init : new qx.data.Array(), apply : "_applyConnectionsIn"},
connectionsOut : {check : "qx.data.Array", event: "ChangeConnectionsOut", init : new qx.data.Array(), apply : "_applyConnectionsOut"},
model : {check : "Object", event : "ChangeModel", init : new Object(), apply : "_applyModel"},
modelString : {check : "String", event : "ChangeModelString", init : new String(), apply : "_applyModelString"},
modelDebug : {check : "String", event : "ChangeModelDebug", init : new String()}
},
events :
{
},
members :
{
//loads data from state and stores it in properties->data according to the stateKeys specified in this object
receiveData : function (){
if(typeof this.__stateKeys != "object" || typeof this.__state != "object") return false;
for(var i = 0; i < this.__stateKeys.length; i++){
var stateKey = ""+this.__stateKeys[i];
var stateValue = ""+this.__state.get(stateKey);
if(stateKey == this.__modelKey){ //process model!
this.setModelString(stateValue);
}
var connectionsIn = this.getConnectionsIn();
if(connectionsIn.getItem(i) != stateValue){
connectionsIn.setItem(i, stateValue);
this.debug("Updated ConnectionsIn[" + i + "] = " + stateValue);
}
}
return true;
},
_applyModel : function (value, old) {
this.debug("ApplyModel! vlaue: " + value + " old: " + old);
},
_applyModelString : function (value, old) {
this.debug("ApplyModelString! vlaue: " + value + " old: " + old);
//ModelString is going to be converted to Model
try {
value = value.replace(/'/g, "\"");
//alert(value);
var model = qx.util.Json.parse(value);
this.setModel(model);
var backtostring = qx.util.Serializer.toJson(model);
this.debug("CONVERTED BACK TO STRING: " + backtostring);
}catch(e){
//TODO: should build some fallback model!
this.debug("ERROR: building model failed! "+ e.message); return false;
}
},
_applyConnectionsIn : function (value, old) {
//this.debug("ApplyConnectionsIn! vlaue: " + value + " old: " + old);
},
_applyConnectionsOut : function (value, old) {
//this.debug("ApplyConnectionsOut! vlaue: " + value + " old: " + old);
},
buildModelFromData : function () {
return false;
},
processStateUpdate : function() {
this.__state = this.__wave.getState();
this.receiveData() ? this.debug("receiving data... OK") : this.debug("receiving data... ERROR");
},
//submits delta | TODO: should register the used styte keys
submitEventDelta : function (delta){
this.__state.submitDelta(delta);
for (var id in delta){
this.debug("Submit Delta with KEY: " + id + " Value: " + delta[id]);
this.setModelDebug("Submit Delta with KEY: " + id + " Value: " + delta[id]);
}
}
}
});