4 Replies Latest reply on Feb 16, 2011 1:05 PM by Łukasz Grela

    Is this a way of instantiating a class without using ActionScript?

    cheftimo Level 2

      I am working on a project based on the sample Flextore application that many of you are familiar with. If you don't know the project, it can be seen here: http://www.adobe.com/devnet/flex/samples/flex_store.

       

      In that project, there is a class called ProductFilter, written entirely in AS. The class is in package samples.flextore; this package contains a total of six classes, all AS files

       

      Talking about a class like this one, normally, one would

       

      import samples.flexstore.ProductFilter;

       

      then use the class in one or more functions within the <mx:Script> block of the mxml file.

       

      But in this mxml file – a custom component called ProductFilterPanel.mxml, the class is NOT imported. Instead they declare a new namespace like so,

       

      xmlns:flx="samples.flexstore.*",

       

      and then, below the <mx:Script> block is this mxml code block, which looks to me like the creation of an instance of the ProductFilter class:

       

      <flx:ProductFilter id="filter">

           <flx:series>{series.selectedItem}</flx:series>

           <flx:minPrice>{sliderValue(priceSlider.values0)}</flx:minPrice>

           <flx:maxPrice>{sliderValue(priceSlider.values, 1)}</flx:maxPrice>

           <flx:triband>{cbTriband.selected}</flx:triband>

           <flx:camera>{cbCamera.selected}</flx:camera>

           <flx:video>{cbVideo.selected}</flx:video>

      </flx:ProductFilter>

      (series, minPrice, masPrice, etc. are all properties - vars - declared in the AS class file).

       

      My question: is this an alternative way to use a class and an instance of it? Like instead of "var filter:ProductFilter = new ProductFilter;"?

       

      If so, what is this kind of code block called, and is it documented somewhere? I have never seen anything like this in ASDoc or any other mxml documentation.

       

      If what I think is right, when and where is it better to code an mxml component like this (instead of importing and writing a function)?

       

      I would love to hear what this is all about.

       

      Carlos

        • 1. Re: Is this a way of instantiating a class without using ActionScript?
          jake_flex Level 2

          To me, this is exactly what mxml is all about: writing code in xml.

           

          You are comparing import statetement and taking a namespace in to use in mxml tag, but they are the same thing. import statement takes a namespace in to use.

           

          For the question which one is better, I don't think that there's a clear answer. In my opinion using only xml tags in mxml components is more readable compared to using the script tag, but for some dynamic functionalities you need ActionsScript and the script tag. In many cases it's just a matter of taste.

          1 person found this helpful
          • 2. Re: Is this a way of instantiating a class without using ActionScript?
            cheftimo Level 2

            I think I got it. I am just surprised that there is so little documentation about it; If I had not gone through the code and analyzed it, I would have never thought of using, say a <flx:series> tag, which would be unlikely to show up in any search.

             

             

             

            "To me, this is exactly what mxml is all about: writing code in xml" is something I agree with. I guess which one is better is up to the code writer to decide, but being aware of this possibility really helps in grasping the concept of an mxml tag or component being an AS class. Now I realize some things I had never thought of before.

             

             

             

            jake_flex, thanks for enlightening me.

             

             

             

            Carlos

            • 3. Re: Is this a way of instantiating a class without using ActionScript?
              jake_flex Level 2

              You're welcome Carlos.

               

              One more thing. Previously I said it's a matter of taste, which is true, but there are of course preferences. mxml is for mainly static stuff like creating the UI and ActionsScript for the more dynamic stuff and operations behind.

               

              Creating an UI with ActionsScript only requires a lot of code as is messy compared to mxml.

              • 4. Re: Is this a way of instantiating a class without using ActionScript?
                Łukasz Grela

                Hi jake flex,

                 

                I can't agree with your statement that creating with pure AS3 is messy, or requires more code, in fact is quite opposite, and because you can't see the code behind fascade of XML tags it doesn't mean it is not there,

                 

                I can create a button and put it on stage with fair amount of code then export SWF (using AS3 only) and I can do the same with flex which SWF will be larger?

                 

                problem is when I want to create more complex functionalities taht flex already have for you then I have to create more code, but that's OK as always I can use some framework or try to create my own, trimmed to fit my needs (without bulk of not used code).

                 

                 

                best regards