1 Reply Latest reply on Mar 29, 2012 8:40 AM by bucpatr1

    MVC pattern in flash builder

    jerry98225 Level 1

      I am trying to figure out the MVC patter in flash builder. I understand we can seperate view and controller by building custom components as view and populate them in the controller file. However, I don't know how to apply the Model login in flash builder. Do i create a Model folder and file and using that file to receive data?

       

      For example:

       

      Main controller:

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

      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                                       xmlns:s="library://ns.adobe.com/flex/spark"

                                       xmlns:mx="library://ns.adobe.com/flex/mx"

                                       minWidth="955" minHeight="600"

                                       xmlns:components="components.*">

                <fx:Declarations>

                          <!-- Place non-visual elements (e.g., services, value objects) here -->

                          <s:HTTPService url="test.come" id="testData" result="testData_resultHandler(event)"/>  //Data here

       

                </fx:Declarations>

       

                <fx:Script>

                          <![CDATA[

                                    import mx.collections.ArrayCollection;

                                    import mx.rpc.events.ResultEvent;

       

                                    public var testData:ArrayCollection;

       

                                    protected function testData_resultHandler(event:ResultEvent):void

                                    {

                                              testData=event.result.something...   //How to seperate the Data with the main controller?????

                                    }

       

                          ]]>

                </fx:Script>

       

      //view

                <components:videoList />

           <components:videoList2 />

           <components:videoList3 />

       

      </s:Application>

       

      I appreciate any replies. Thanks a lot.

        • 1. Re: MVC pattern in flash builder
          bucpatr1 Level 2

          The usual pattern in flex is to have an mxml component 'the view', an actionscript class (the controller), and some additional actionscript classes that represent the data objects (the model). Your 'main' application class is responsible for creating a new instance of the controller, usually as response to the creationComplete event. The controller then initiallizes/retrieves the 'Model' objects and passes them back to the view which binds them to the appropriate fields. So at the bare minimum for an MVC project your are going to have Main.mxml, MainController.as, and Model.as.

           

          This is not the most elegant solution since it tends to create tight coupling between the controller and the view. If you want to use the MVC pattern on any project that involves more than 3 or 4 simple views you will probably want to look into a custom MVC framework like Cairingorm or the Tide framework from GraniteDS. Make sure you have a solid understanding of the basics first though.