Archive for April, 2009
Data Model Inpector (peek)
Posted by: | CommentsI recently built a console for inspecting arbitrary values contained in my data model. I wanted something similar to the Flex Builder “Variables” window, and though I haven’t arrived there quite yet, I’ll share what I’ve got so far.
The mechanics are a little boring: I extended TitleWindow, there is a TextInput where I enter the value I want to inspect and a TextArea where the ObjectUtil.toString() dump of the object is output.
The heart of the control is this method:
private function peek(prop : String) : void {
try {
var a : Array = prop.split('.');
var x : * = model;
for (var i : int = 0; i < a.length; i++) {
x = x[ a[i] ];
}
console.text = ObjectUtil.toString(x);
}
catch (error : Error) { }
}
It accepts a “modified-dot-notation” String, parses it, and constructs the result using model as the root.
I knew I was going to use the bracket operator. In fact I started out spliting my tokens and accessing the values like this: model[ a[0] ] [ a[1] ] [ a[2] ]... and so on. It wasn’t very scalable, which is why I was pleased with this method.
The interesting thing about it is that because ArrayCollections allow for use of the bracket operator, accessing elements of an ArrayCollection is simple. Instead of "acName.list.source.2", you can pass the method "acName.2” and it returns the correct value.

