8 Replies Latest reply on Jan 25, 2010 10:55 PM by archemedia

    Calling functions between mxml files

    bentwonk

      I have a project with muliple MXML files, and script fuctions embedded in each file.

      I would like to be able to call a script function in one MXML file from a sript in a second MXML file, and don't seem to be able to do this, certainly missing something, being new I expect there is a gap in my understanding.

      thanks
      Ben

        • 1. Re: Calling functions between mxml files
          Sebastien V. Level 3

          Mxml being class instances, you will need a reference to the targeted mxml in the one from which you wish to call the method.

           

          In other words, the second mxml (calling the method on the first) should have access to the first one. This is the case when the called one is a child of the calling one, but this is way less simple in most of the cases.

           

          Usually you need to find a common parent mxml, and make it call the second mxml method on a first mxml event dispatch.

           

          Hope this helps.

          1 person found this helpful
          • 2. Re: Calling functions between mxml files
            bentwonk Level 1

            Is there a good tutorial that goes into this in depth?, I seem fine in AS3, and MXML is easy enough to understand, but I am having difficulty getting my head around making them play well together when not in the same file.

             

            thanks.

            • 3. Re: Calling functions between mxml files
              archemedia Level 4

              This is a common design principle. If you need functions which are accessible from anywhere, you'd better adopt to the singleton pattern.

              I posted a message on this: http://forums.adobe.com/thread/562192?tstart=0

               

              In this post, you'll see that there is a property in the singleton class which is available througout your application. You can however add public functions to this class which are available also. You might name this class as 'GlobalController' as it adopts to the mvc pattern (Model, view, controller).

               

              You can then access its functions through:

               

              GlobalController.getInstance().myFunction();

               

              You can even add event listeners on this class which call functions in your mxml components.

               

              If you can't figure it out with this info, I'll make a small example if you wish

               

              Dany

              • 4. Re: Calling functions between mxml files
                pauland Level 4

                bentwonk wrote:

                 

                Is there a good tutorial that goes into this in depth?, I seem fine in AS3, and MXML is easy enough to understand, but I am having difficulty getting my head around making them play well together when not in the same file.

                 

                thanks.

                 

                mxml components are just class descriptions. You just need to consider them as classes. If an mxml tag is used with an ID, the component tag identifies the class and the id the instance. At the application level the situation is exactly the same - you are writing an application class that is instantiated when the app runs.

                 

                If you are compfortable with AS3 classes, you should bear the above in mind and see that there is no great difference between MXML and AS3 - particularly with regard to function calls. MXML is a descriptive shorthand for AS3.

                 

                Whatever the problems that you have in calling functions inside MXML components, would be the same if you had just used AS3.

                 

                Paul

                1 person found this helpful
                • 5. Re: Calling functions between mxml files
                  bentwonk Level 1

                  Thanks, I did read your singleton post, and will probably use tehm in the future. :-)

                  • 6. Re: Calling functions between mxml files
                    bentwonk Level 1

                    Thanks Paul, that clarifies things for me a bit, I think I need to write a few simple test programes to get my head around the

                    interaction. I have a couple of books on Flex, but they seem to skim over the 'scalling to a real world size project'.

                    • 7. Re: Calling functions between mxml files
                      John Hall Level 4

                      Can't wait for the anti-singleton folks to post. It's always an interesting conversational duel.

                      • 8. Re: Calling functions between mxml files
                        archemedia Level 4

                        In fact, putting all your global properties and methods on your application level does the same as using a singelton class. In most cases, you only have one application (unless you load other swf's dynamically). In large applications however, using singleton classes as models and controllers, things get much clearer and updatable. If you're interested, read some articles cairngorm.

                         

                        Dany