开发者

Flex Component using global variable

I am using Flash Builder 4 Burrito Preview - builing a Mobile Application. I have a custom component called footer.mxml. That footer has 4 buttons, and one of them has a lable that is bound to cartValue. I am trying to maintain a global variable called cartValue within all views and footer component.

footer.mxml

<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
   xmlns:s="library://ns.adobe.com/flex/spark"
   xmlns:mx="library://ns.adobe.com/flex/mx"
   width="100%" height="64" chromeColor="#000000" fontSize="10">
<fx:Declarations>
  <!-- Place non-visual elements (e.g., services, value objects) here -->
  <mx:CurrencyFormatter id="currencyFormatter"
     currencySymbol="$"
     useThousandsSeparator="true"
     precision="2" />
 </fx:Declarations>



<fx:Script>
  <![CDATA[
   [Bindable]
   public var cartValue:int;

  ]]>
</fx:Script>

 <s:HGroup width="100%" contentBackgroundColor="#000000" paddingBottom="0" paddingLeft="0"
 paddingRight="0" paddingTop="0">
 <s:Button x="0" y="624.5" width="25%" height="64" label="Accoun开发者_如何学编程t" chromeColor="#2259AA"
 enabled="true" fontSize="10" fontWeight="bold" icon="@Embed('assets/user.png')"/>
 <s:Button x="121" y="624.5" width="25%" height="64" label="Orders" chromeColor="#2259AA"
 fontSize="10" icon="@Embed('assets/doc_lines_stright.png')"/>
 <s:Button x="241" y="624.5" width="25%" height="64" label="Help" chromeColor="#2259AA" 
 fontSize="10" icon="@Embed('assets/spechbubble.png')"/>
 <s:Button x="360" y="624.5" width="25%" height="64" label="{currencyFormatter.format(cartValue)}" chromeColor="#2259AA" 
 fontSize="10" icon="@Embed('assets/shop_cart.png')"/>

 </s:HGroup>
    </s:Group>

RincoTest.mxml

 <s:MobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark"
  xmlns:mx="library://ns.adobe.com/flex/mx"
  backgroundColor="#000000" firstView="views.RincoTestHome"
  >
 <fx:Style source="RincoTest.css"/>
<fx:Declarations>
 <!-- Place non-visual elements (e.g., services, value objects) here -->
 </fx:Declarations>
 <fx:Script>
  <![CDATA[

   [Bindable] 
   public var cartValue:int;
  ]]>
 </fx:Script>


 <s:titleContent>
  <s:Image left="1" top="3" width="173" height="75" backgroundAlpha="1.0" smooth="true"
 source="assets/iphone_large.png"/>
 </s:titleContent>
 <s:navigationContent>
  <mx:Spacer width="10" height="82"/>
 </s:navigationContent>


    </s:MobileApplication>

And this is how I am implementing it

<components:footer x="1.65" y="614.95" width="100%" height="64" cartValue="{cartValue}"/>

I have tried to bind Application.application.cartValue and MobileApplication.application.cartValue. Neither of them work.

If there is a better way to maintain a cartValue across the entire application please let me know. This is my first attempt with Flex.

Thanks, Ernie


Use a static variable in the component.

Reference it as ComponentName.staticVar. Usually, everything that needs to know about the global already knows about (e.g. imports) the component file.

Cheers


Another solution is using a singleton pattern: a single instance that everything references through a static accessor.

The effect is about the same. The only advantage to a static reference to an instance over static properties is that the instance can be part of an inheritance and can fulfill an interface.

I've also had some intermittent problems with binding to static values, but that might have been a previous version, PEBCAK, etc.

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜