3 Replies Latest reply on Mar 27, 2008 7:26 PM by ntsiii

    pass variable from main to component

    Astronax
      Is this a good way to pass data from main to a component? if not how would you do it.

      Thanks


      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"/>
      <mx:Script>
      <![CDATA[
      IDMYMENU.varInComponent = "....";
      ]]>
      </mx:Script>
      <MYMenu:TopMenuApplicationBar id="IDMYMENU"/>
      </mx:Application>


      <mx:ApplicationControlBar xmlns:mx=" http://www.adobe.com/2006/mxml" >
      <mx:Script>
      <![CDATA[
      var varInComponent:String;
      ]]>
      </mx:Script>
      </mx:ApplicationControlBar>
        • 1. Re: pass variable from main to component
          ntsiii Level 3
          Certainly that is ok. varInComponent is known as a property of the component. You will want to make that var "public", though

          You can also use getter and setter functions if you want more control over what happens when that value is set, for example, if you wanted to run some other code.

          Another way is to pull the data into the component using Application.application.myPublicMember;

          Still another way is to use events to listen for a change of a value in the main app. then you can access any public member of the app using the event.target reference.

          You can also pass data in custom events.

          Yet another solution is to use a data model component, often implemented as a "singleton". Such a model component can be accessed anywhere in the app, and can be used to invoke bindings or trigger ChangeWatchers.

          You have many options.

          Tracy
          • 2. Re: pass variable from main to component
            slaingod Level 1
            As much as everyone likes using singleton's in their apps, I find its use completely screwing up good design for app reusability. I know this sounds counterintuitive, but I've got a full Caringorm app here. Now I am in the position of needing to have 2 of them in another app. Think media player: The media player was written using cairngorm and works all fine and dandy on its own. But as soon as you try to make it a module/include it on a page that has 2 media players, all that singleton model locator has bought you is not much.

            Cairngorm and a lot of these other MVC frameworks make a lot of assumptions about the types of apps you are creating: single instance standalone for the most part. Which is just a bad assumption in my experience...mashing things up into a bigger app is a fairly common occurence.

            For me, to get around the limitations, I have to use SWFLoader and a bunch of other stuff to load my media player swf into another flex swf.

            • 3. Re: pass variable from main to component
              ntsiii Level 3
              I agree.

              Of course, the importance of "resusability" is another assumption.

              There is no single "framework", or "pattern" or "methodology" or "best practice" that is correct for all.

              Always proceed with eyes open.

              Tracy