5 Replies Latest reply on Dec 7, 2010 5:46 AM by MartinHviid

    MXML inheritence / injection

    MartinHviid Level 2

      Hi

       

      In Flex 3 I did inheritence / injection, into my main file.

      The reason I could do this were because of how states worked in Flex 3, the way I did it was as follows:

       

      I had my main app, were I did all my general MXML which had the Application tag, somewhere I had a <VBox id="main" />

       

      And the i my sub class, I inherited from my general MXML file, and overrided the states Array:

       

      <local:states>

        <mx:State name="ansoger_bedemand_privatperson">

          <mx:AddChild relativeTo="{main}" position="lastChild" creationPolicy="all">

            <states:ansoger_bedemand_privatperson />

          </mx:AddChild>

        </mx:State>

      </local:states>

       

      And that way injected states into my general layout.

       

      Are there any ways doing anything similar in Flex4?

       

      I have tried:

      General application MXML

      <s:VGroup id="main">

       

      </s:VGroup>

       

      Inherited file:

      <local:main>

      <s:Label includeIn="someState" text="sdf" />

      </local:main>

       

      But that doesn't work.

       

      I wanna achieve this kind of abstraction, so the developer developing the sub class, never have to touch the base class.

       

      Best Regards

      Martin Andersen

        • 1. Re: MXML inheritence / injection
          JeffryHouser Level 4

          I am quite confused by your explanation of what you've done in Flex 3, perhaps you should try to expand on what you're trying to accomplish a bit.

           

           

          MartinHviid wrote:

           

          Hi

           

          In Flex 3 I did inheritence / injection, into my main file.

          The reason I could do this were because of how states worked in Flex 3, the way I did it was as follows:

           

          I had my main app, were I did all my general MXML which had the Application tag, somewhere I had a <VBox id="main" />

           

           

          So, in Flex 3 you had a main application like this:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">

          <VBox id="main" />

          </mx:Application>

           

           

          And the i my sub class, I inherited from my general MXML file, and overrided the states Array:

           

          <local:states>

            <mx:State name="ansoger_bedemand_privatperson">

              <mx:AddChild relativeTo="{main}" position="lastChild" creationPolicy="all">

                <states:ansoger_bedemand_privatperson />

              </mx:AddChild>

            </mx:State>

          </local:states>

           


           

          In what sub class?   What general MXML File are you inheriting from?  Where does the 'local' prefix come from in your code snippet?  This syntax looks like you a defining values for the states array in MXML, which has nothing to do with the concept of overriding.

           

          And that way injected states into my general layout.

           

          Are there any ways doing anything similar in Flex4?

           

          I have tried:

          General application MXML

          <s:VGroup id="main">

           

          </s:VGroup>

           

          Inherited file:

          <local:main>

          <s:Label includeIn="someState" text="sdf" />

          </local:main>

           

          But that doesn't work.

           

          Please quantify "Does't work."  Are you getting a run time error?  Or compile time error?  Or  experiencing something else?  Based on your code snippets, you can't include the label in "someState" because you never defined "someState" as a state.

          • 2. Re: MXML inheritence / injection
            MartinHviid Level 2

            Hi Jeffry

            Thank you VERY MUCH for taking the time to answer my question.

             

            I'll try to elaborate.

             

            I'm building some guided forms, and I have a library project were I have a MXML file, lets call it main, that's sub classed from Application.

            In main.mxml, I put all my general layout / design. Then I have a content area.

             

            Then from different guide projects, I wan't to be able to sub class main.mxml and then inject my content into the container in main.mxml.

             

             

            I'm just gonna post an example of how I did in Flex 3 on pastebin, so you get syntax highlighting.

            http://pastebin.com/612vPgPs

             

            What I have tried in Flex 4:

            http://pastebin.com/wfiek42x

             

            I can't atm remember if I is a runtime error, or Flash Builder syntax error, but I'll come back with an answer, I'm just about leaving now.

             

            Thanks very much

            Martin Andersen

            • 3. Re: MXML inheritence / injection
              JeffryHouser Level 4

              I bet you're getting compile time errors.  In your Flex 4 example you are not defining a namespace for 'local'.

               

              I can understand untuitively why your Flex 4 version doesn't work, but I'm not sure how to desribe it.

               

              The way you define states is not overriding anything, as you said it might be.  It is just an MXML way to set the value.

               

              It appears in the Flex 4 sample that your class name "main" has a property named "main". ( I see the same thing in the Flex 3 sample).  That is unusual to me, and it may cause some issues.

               

              I would expect that defining "local:main" would cause issues because are you trying to create a new instance of the component 'main'?  Or are you trying to set values to the component's property 'main'?  How can the Flx Compiler tell?  I would not recommend creating a component that has the same name as a property.

               

              While your 'main' subclass component does inherit a value named 'main' from the parent, I do not think you can define children to that component in the manner you're trying to.  You could do it in ActionScript using the addElement method.

               

              After all is said and done, it isn't obvious to me what the benefit of this approach is.

              1 person found this helpful
              • 4. Re: MXML inheritence / injection
                MartinHviid Level 2

                Hi Jeffry

                 

                No I know it's not overriding, I guess I wrote that a little bad.

                 

                Yeah, I just wrote the examples by hand and afterwards saw that I gave both the Class and a property the same name.

                 

                I'm trying to set a value for the component main. But yeah sure I see your point there.

                 

                I tried doing it in ActionScript with a setter in the main class:

                 

                public function set mainContent(value:Array):void

                {

                     for each(var element:IVisualElement in Array)

                     {

                          main.addElement(element);

                     }

                }

                 

                I also tried with just a single IVisualElement

                 

                 

                The benefit here should be that all the layout I wanna reuse, like a header, menu, footer etc. and have all this in a super class.

                So I didn't have to copy and paste code.

                 

                Like (half Flex half pseudo here)

                 

                <Application>

                     <Header />

                     <Menu />

                     <VGroup id="content" />

                     <Footer />

                </Application>

                 

                <SubClass>

                // Somehow inject this into super class

                </SubClass>

                 

                So why not just implement the states in the super class?

                Because I have several projects that I wan't to use the same layout on.

                 

                Thanks

                Martin

                • 5. Re: MXML inheritence / injection
                  MartinHviid Level 2

                  Hi everybody

                   

                  Thanks for spending time on this problem, I haven't really solved it yet, but I have gotten a better idea of the problem. I'm trying to achieve templating, and being able to use states in the sub class.

                   

                  So I will be closing this thread, and starting a new one, with my new knowledge.

                   

                  Thanks

                  Martin