In an effort to begin supporting complex property syntax within ZTL methods (let's say an Array of structures with 2 properties: prop1 and prop2), I have a question...
Today, I've seen where Karol Kalisz can declare complex types in ZTL as below in a simple example:
/**
* Complex Item Def
**/
class example.ComplexProperty extends Object { String prop1; int prop2;
}
/**
* Array Def
**/
class example.ComplexArray extends Array { example.ComplexArray (ComplexProperty p);
}
/**
* Component Def
**/
class example.MyComponent extends Component { example.ComplexArray getComplexProperties() {* var arr = JSON.parse(this.complexProperties); // String serialized as in terms of DS Property storage, so let's parse to Array in Rhino JS return arr; *}
}So this works today, in BIAL i can say something like this as a result:
var a = COMPONENT_1.getComplexProperties();
a.forEach(function(element, index) { APPLICATION.alert(element.prop1);
});The result it an alert (or whatever real logic in a real-world case) for each prop1 String in my Array.
Here's my Part 1 of the question... Can i MODIFY prop1? It seems I cannot.
var a = COMPONENT_1.getComplexProperties();
a.forEach(function(element, index) { element.prop1 = "Hello";
});This causes a BIAL error for reasons that I don't understand. Perhaps it's just plain not supported or I'm missing something... So that's my question, is this even possible or is this a "read-only" case where I can act on the information but cannot update the property in this manner?
Part 2:
I've managed to work around this with some complex getter/setter logic by replacing the class ComplexProperty ZTL definition with this:
class example.ComplexPropertyConfig extends Object { void setProp1(String s) {* *} void setProp2(int i) {* *} String getProp1(* *} int getProp2{* *}
}This allows the templating code to make BIAL recognize the method signatures, and then we fill in the passing of the properties in the Component definition:
// Inside example.MyComponent...
example.ComplexPropertyConfig getComplexPropertyConfig(int index) {* var that = this; var arr = JSON.parse(this.complexProperties); // Deserialize to array. var obj = arr[index]; var retObj = { setProp1 : function(s){ obj.prop1 = s; var newArr = JSON.parse(that.complexProperties); newArr[index] = obj; that.complexProperties = JSON.stringify(newArr); } getProp1 : function(){ return obj.prop1 } setProp2 : function(i){ obj.prop2 = i; var newArr = JSON.parse(that.complexProperties); newArr[index] = obj; that.complexProperties = JSON.stringify(newArr); } getProp2 : function(){ return obj.prop2 } } return retObj;
*} Now, this works - I am able to then do this in BIAL...
var a = COMPONENT_1.getComplexProperty(0); // Get property of array at 1st index
a.setProp1("Hello");I even have this working in a more complex example to also return the Array equivalent and you can even forEach over each one and call the setter and getter, etc.. But I'm trying to keep this example simple...
So, back to my question for Part 2....
I have the suspicion that my method in 'getComplexPropertyConfig' will cause an eventual memory leak as I am technically creating a new retObj each time. What I'd LIKE to do instead, is perform this retObj setup during initialization or Construction of the instance of example.MyComponent if this is possible. This would allow me to account for these dangling objects and functions referencing back to obj.
Is there a way in ZTL to establish a Constructor method that could basically do the retObj defining in method 'getComplexPropertyConfig'? This would allow me to do it once, and not every time I issue a get call (again, I fear memory leak.)
Questions? Thoughts? Looking for a hardcore ZTL expert here ![]()