3 Replies Latest reply on Mar 30, 2009 9:50 AM by Newsgroup_User

    Where do you write your code?

    haagel
      I've just started to learn Flex. One question I have is where you put the ActionScript code?
      I know i can put it in several locations (e.g. inline, within a script-tag, in actionscript-files, in actionscript classes) but what's the best way to go?

      Up until now I haven't done so much coding, so I have just written the code inside a <mx:Script>-tag in my MXML-file. I'm starting to realize though that having both MXML and ActionScript in the same file is a bit inconvinient when the amount of code increases.

      Since I come from the dark side ;) (Microsoft), I'm used to having to files for each page/component; one with the "UI code" (MXML in this case) and one with the logic (AS in this case). I like that model, so I tried to do something similiar in Flex. My MXML-file looks like this:

      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="MainCode()">
      <mx:Script source="MainCode.as" />
      <!-- Other MXML code -->
      </mx:Application>

      Then I create the MainCode.as and give it a function called MainCode(). That way, I now have the code in a separate file/class, and themethod becomes the starting point of the execution. Kind of like the "document class" in Flash.

      First, this seemed like a good solution to me BUT soon I realized it has a big flaw. Since the class is not hard linked to the MXML, I will not get any intellisense/codehints about the components/controls I use in the MXML (at least I don't in FlashDevelop).

      So, how do you do it? Is there a way that is considered "best practice"?
        • 1. Re: Where do you write your code?
          sminrana Level 1
          its best to separate your application into modules and components i am talking about Big application where each module and component can have their own AS code and MXML tag
          • 2. Re: Where do you write your code?
            sminrana Level 1
            its best to separate your application into modules and components i am talking about Big application where each module and component can have their own AS code and MXML tag
            • 3. Re: Where do you write your code?
              Level 7

              "haagel" <webforumsuser@macromedia.com> wrote in message
              news:gqqeqj$7s2$1@forums.macromedia.com...
              > I've just started to learn Flex. One question I have is where you put the
              > ActionScript code?
              > I know i can put it in several locations (e.g. inline, within a
              > script-tag, in
              > actionscript-files, in actionscript classes) but what's the best way to
              > go?
              >
              > Up until now I haven't done so much coding, so I have just written the
              > code
              > inside a <mx:Script>-tag in my MXML-file. I'm starting to realize though
              > that
              > having both MXML and ActionScript in the same file is a bit inconvinient
              > when
              > the amount of code increases.
              >
              > Since I come from the dark side ;) (Microsoft), I'm used to having to
              > files
              > for each page/component; one with the "UI code" (MXML in this case) and
              > one
              > with the logic (AS in this case). I like that model, so I tried to do
              > something
              > similiar in Flex. My MXML-file looks like this:
              >
              > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
              > layout="absolute"
              > applicationComplete="MainCode()">
              > <mx:Script source="MainCode.as" />
              > <!-- Other MXML code -->
              > </mx:Application>
              >
              > Then I create the MainCode.as and give it a function called MainCode().
              > That
              > way, I now have the code in a separate file/class, and themethod becomes
              > the
              > starting point of the execution. Kind of like the "document class" in
              > Flash.
              >
              > First, this seemed like a good solution to me BUT soon I realized it has a
              > big
              > flaw. Since the class is not hard linked to the MXML, I will not get any
              > intellisense/codehints about the components/controls I use in the MXML (at
              > least I don't in FlashDevelop).
              >
              > So, how do you do it? Is there a way that is considered "best practice"?

              It's better to do one of two things:

              1) Get everything pretty much complete and then only pull it out to an as
              file when you're done. That way you get code hinting all the way up to the
              time that you pull out the code. Note that this will _not_ be a class and
              should not have a constructor, etc. or be named the same thing as any MXML
              file in your project.
              2) Only pull out things that can truly stand alone, and make them separate
              class. You need to look up these terms on google or your favorite search
              engine "loosely coupled Flex components."

              Also note that you can use more than one script tag, and these will be
              visible in outline view. Unfortunately you can't give them ID's. Also,
              using bookmarks can help with managing files with both MXML and AS.