Ok, I wouldn't feel wrong about this approach. We use a similar approach
within an application at work where a custom viewstack is setting one of its
children to "visible", thereby causing the show event to fire. We rely on
this to trigger some code that sets up the view properly based on changes
that might have happened when the view was hidden.


Tim


On 3/26/07, wdsnapper <[EMAIL PROTECTED]> wrote:

  It does need to be run each time this Canvas gains focus. The short
explanation is, one canvas has merchandise items available for sale.
An item is marked as "in cart" when it is added do prevent a user from
adding an item multiple times that is only allowed to be in the cart
once. The cart lives on another Canvas in the stack. A user could
choose to remove an item from the cart and then select that
merchandise Canvas again and the button needs to be "reactivated" and
have its label and style changed.

Therefore the only way I knew to make this happen within the Flex
model is to use a "show" event that triggered and caused the Canvas to
update itself.

--- In [email protected] <flexcoders%40yahoogroups.com>, "Tim
Walling" <[EMAIL PROTECTED]> wrote:
>
> If you need to run this code each time your view "gains focus" as
you put
> it, then I think using the show event is fine because that should be
> triggered each time the viewstack sets a new child to be visible. If
this
> code really only needs to run once, why don't you have your
component just
> listen to its own creationComplete or initialize event?
>
> <view:MyComponent initialize="handleInit()>
> <mx:Script>
> private function handleInit():void
> {
>
> }
> </mx:Script>
> </view:MyComponent>
>
>
>
>
>
> On 3/26/07, wdsnapper <[EMAIL PROTECTED]> wrote:
> >
> > Thanks Tim. I initially tried using the "show" event on the
> > component's canvas but it never got invoked. Then I realized that I
> > had a canvas in the view stack that included my component:
> >
> > <mx:ViewStack>
> > <mx:Canvas id="myComponent1">
> > <view:MyComponent/>
> > </mx:Canvas>
> > <mx:Canvas id="myComponent2">
> > <view:MyComponent/>
> > </mx:Canvas>
> > <mx:Canvas id="myComponent3">
> > <view:MyComponent/>
> > </mx:Canvas>
> > <mx:Canvas id="myComponent4">
> > <view:MyComponent/>
> > </mx:Canvas>
> > </mx:ViewStack>
> >
> > <view:MyComponent>
> > <mx:Script>
> > <![CDATA[
> > private function myShowMethod1():void {
> > }
> > private function myShowMethod2():void {
> > }
> > ]]>
> > </mx:Script>
> >
> > <mx:ViewStack>
> > <mx:Canvas id="myCanvas2" show="myShowMethod1()">
> > <view:MyView1/>
> > </mx:Canvas>
> >
> > <mx:Canvas id="myCanvas2" show="myShowMethod2()">
> > <view:MyView2/>
> > </mx:Canvas>
> > </mx:ViewStack>
> > </view:MyComponent>
> >
> > I was expecting the myShowMethod1() and myShowMethod2() to be invoked
> > when the view:MyComponent was brought into view and this was not
the case.
> >
> > I ended up hooking the show event in "myComponent1" and had this
> > invoke a method in view:MyComponent and this worked.
> >
> > Is there a way to make sure the "show" gets invoked on my component?
> >
> > The problem is solved for now though just does not feel like the right
> > way.
> >
> > Thanks.
> >
> > --- In [email protected] 
<flexcoders%40yahoogroups.com><flexcoders%40yahoogroups.com>, "Tim
> > Walling" <tim.walling@> wrote:
> > >
> > > You could try setting up a show event listener to trigger your
> > > initialization code. Something like this:
> > >
> > > <mx:ViewStack>
> > > <mx:Canvas show="handleShowEvent()"/>
> > > </mx:ViewStack>
> > >
> > >
> > > Whenever the ViewStack makes the canvas the visible/active one,
the show
> > > event would fire.
> > >
> > > or if your Canvas is an mxml component, it could handle the show
event
> > > internally:
> > >
> > > <mx:ViewStack>
> > > <view:MyCanvas/>
> > > </mx:ViewStack>
> > >
> > > MyCanvas.mxml
> > > <mx:Canvas show="handleShowEvent()">
> > > <mx:Script>
> > > private function handleShowEvent(event:FlexEvent):void
> > > {
> > > // call initialize code
> > > }
> > > </mx:Script>
> > > </mx:Canvas>
> > >
> > > Then each of your components in the ViewStack would manage their
> > respective
> > > show events.
> > >
> > >
> > > Tim
> > >
> > >
> > > On 23 Mar 2007 17:02:56 -0700, wdsnapper <bill@> wrote:
> > > >
> > > > I am using a ViewStack to contain a set of Canvas'. I switch
between
> > > > items in the stack by clicking on buttons. I want to be able to
> > > > detect when a Canvas gains focus and run a method to initialize
> > > > content on that Canvas.
> > > >
> > > > What is the correct event to monitor to achieve this behavior?
> > > >
> > > > Thanks in advance.
> > > >
> > > >
> > > >
> > >
> > >
> > >
> > > --
> > > Tim
> > > timwalling.com
> > >
> >
> >
> >
>
>
>
> --
> Tim
> timwalling.com
>




--
Tim
timwalling.com

Reply via email to