Archive for the 'code' Category

03
Apr
08

Flex Panel’s Not Scrolling When Expected Fix

I wouldn’t call this a fix, as this is more of a misunderstanding of the Panel component in Flex…simple yes, but it took myself and a coworker a couple minutes to figure it out.
We had a Panel component that sat inside a Container whose height was < the Panel’s height, and we expected the content’s of the Panel to scroll; unfortunately, the entire Panel scrolled so we sat down and created a small example to see how we could get around this.
Check out the following code — in the first Panel, we were hoping the contents (for example purposes TextInputs) would scroll, but instead the entire Panel scrolled. In the second example, we changed the layout property to a value of “absolute,” and this gave us the desired results — the content’s of the Panel scrolled. Note the use of a Box Container around the TextInputs — without this the TextInputs sat on top of each other since we’re telling the Panel to use a Canvas instead of a VBox to house it’s contents.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
	xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute">

	<mx:VBox width="100%" height="100%">

		<mx:Box width="100%" height="100">

			<mx:Panel
				width="100%" height="100%"
				title="Bad :: Unexpected Scrolling">

				<!--
					This Box is unnecessary since there's a VBox in
					a Panel by default, but it's here for consistency
					since it's necessary to get the second Panel to
					scroll as expected.
				-->
				<mx:Box>
					<mx:TextInput />
					<mx:TextInput />
					<mx:TextInput />
					<mx:TextInput />
					<mx:TextInput />
					<mx:TextInput />
				</mx:Box>

			</mx:Panel>

		</mx:Box>

		<mx:Box width="100%" height="100">

			<!--
				This Box is unnecessary since there's a VBox in
				a Panel by default, but since we've specifed
				the layout as absolute, it's a Canvas
			-->
			<mx:Panel
				width="100%" height="100%"
				title="Good :: Expected Scrolling"
				layout="absolute">

				<mx:Box>
					<mx:TextInput />
					<mx:TextInput />
					<mx:TextInput />
					<mx:TextInput />
					<mx:TextInput />
					<mx:TextInput />
				</mx:Box>

			</mx:Panel>

		</mx:Box>

	</mx:VBox>

</mx:Application>
20
Mar
08

Getting Around Unknown Namepsaces in Flex/AS3

In my last post I presented a RegEx solution to remove a pesky namespace from an XML response payload; another possible solution is to use the * notation to specify any namespace like the following:
Any namespace
The following example shows how to get an element or attribute value when any namespace is specified on the element or attribute:
var attributes:XMLList = XML(event.result).*::Description.*::value;
The previous code returns xxx for either one of the following XML documents:
XML document one:
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description>
<rdf:value>xxx</rdf:value>
</rdf:Description>
</rdf:RDF>
XML document two:
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cm="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<cm:Description>
<rdf:value>xxx</rdf:value>
</cm:Description>
</rdf:RDF>
I nabbed this chunk of code from the Flex Developer’s Guide, section “Handling results as XML with the e4x result format.
14
Mar
08

Remove Annoying XML Namespaces in Flex/AS3

My buddy pointed out another approach that I show here — it’s easier and doesn’t actually remove the namespace.
XML namespaces can be a real pain in the a$$ in AS3, so I often just rip them out. I mean think about it…when you get some XML payload back in a web service response, do you really care or need the namespace? Nah….just rip that bad boy out and starting using your XML baby.
Here’s my quick and dirty AS3 XML Namespace Ripper — for the sake of argument and a quick example, just assume this is in a Responder object like a Caringorm business delegate, but it should probably exist in a static XML util:
public function result(response:Object):void
{
var xmlString:String;
var xmlnsPattern:RegExp;
var namespaceRemovedXML:String;
var responseXML:XML;

// get the XML payload
if(ResultEvent(response).result is XMLList)
{
response = ResultEvent(response).result as XMLList;
}
else
{
response = ResultEvent(response).result as XML;
}

// convert it to a string
xmlString = response.toXMLString();

// define the regex pattern to remove the namespaces from the string
xmlnsPattern = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");

// remove the namespaces from the string representation of the XML
namespaceRemovedXML = xmlString.replace(xmlnsPattern, "");

// set the string rep. of the XML back to real XML
responseXML = new XML(namespaceRemovedXML);

// do something with the XML
}
Thanks for the quick refresher on regex Johnny!