December 5th, 2007 by Kyle
Tags: , , , , , , , , ,
Posted in: ActionScript, Flex


XML and ampersands do not mix well.
Flex uses mxml which is XML.
This can lead to issues when writing binding expressions in mxml tags for component properties when you are trying to write logical expressions using ‘and’.

Here is a simple sample that demonstrates how to escape the ampersands in the expression and how to create a logical expression that does not even use ‘and’, but achieves the same result.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
   
    <mx:Script>
        <![CDATA[
            [Bindable]
            public var bOne:Boolean=false;
            [Bindable]
            public var bTwo:Boolean=false;         
        ]]>
    </mx:Script>
   
    <mx:Button label="(click to toggle) bOne: {bOne}" click="bOne=!bOne"/>
    <mx:Button label="(click to toggle) bTwo: {bTwo}" click="bTwo=!bTwo"/>
   
    <mx:Label text="If bOne and bTwo are true, the buttons below will be Enabled"/>
    <!–escape the ampersands for the logical and operator –>
    <mx:Button label="enabled?" enabled="{(bOne &amp;&amp; bTwo)}"/>
    <!–or use this compounded logical statement which will evaluate to the same thing –>
    <mx:Button label="enabled?" enabled="{(bOne ? bTwo : false)}"/> 
   
</mx:Application>
 

Browse the source of this example.
Download a zipfile containing the source to this sample.

This movie requires Flash Player 9




5 Comments »

October 17th, 2007 by Kyle
Tags: , , , , , ,
Posted in: ActionScript, Flex


I recently had an issue from a customer regarding figuring out the correct syntax to get at a few nodes in an XML object that had namespaces using e4x. Now I don’t know about you, but if I don’t use e4x often, I forget things and get rusty. If your e4x-foo is weak, here are a few references that I found useful:

http://www.darronschall.com/weblog/archives/000223.cfm
(In fact Darren has a few e4x entries aside from this one.)

http://www.partlyhuman.com/blog/roger/as3-e4x-rundown

Hope this helps,

-Kyle




No Comments »

April 17th, 2007 by Kyle
Tags: , , , , ,
Posted in: ActionScript, Flex


E4X is a very powerful and easy way to manipulate xml data in Flex.
Of course something so p[powerful can sometimes be confusing when it produces unexpected results.

     <mx:XML format="e4x" id="myXML">
           <order>
              <item id=’1′>
                  <menuName>burger</menuName>
                  <price>3.95</price>
              </item>
              <item id=’2′>
                  <menuName>fries</menuName>
                  <price>1.45</price>
             </item>
          </order>
      </mx:XML>
 

If you wanted to retrieve the price of the burger menuitem above, it is easily done with the e4x expression:

     <mx:XML format="e4x" id="myXML">
          <order>
              <item id=’1′>
                  <menuName>burger</menuName>
                  <menuName>burger</menuName>
                  <price>3.95</price>
              </item>
              <item id=’2′>
                  <menuName>fries</menuName>
                  <price>1.45</price>
             </item>
          </order>
      </mx:XML>
 

Then if you used the same expression, you actually would not get a value returned.
It is not obvious as to why this happens, but of course there is more than one way to skin a cat…
Here is an expression that will work:

    myXML.item.menuName.(text()=="burger").parent().price;
 

Here is a link to a Flex Builder 2.0.1 project (compiled with SDK hotfix1) containing a sample demonstrating the solution described above.




No Comments »