18 Replies Latest reply on Aug 30, 2011 4:25 PM by Kristin95762

    Need to refer back to the parent application

    Kristin95762 Level 1



      I have a main.mxml file calling a titlewindow. The popular method is to use events to send the data back to the main application. However, I have a class file associated with the titlewindow, in which the only job it has is to insert data into a sqlite database. Using an event would negate the use of the class file, but I'd kind of like to keep that if I can. I like not loading up my main.mxml with a lot of code...after having just figured out how to use a class.


      What I need to do is refresh the dataprovider after I close the titlewindow. The only way I know how to do that is to use the event to share the data with the main app, and then run the insertStatement. Right now I have a button on the main.mxml that refreshes the dataprovider.


      Is there a solution to this?





        • 1. Re: Need to refer back to the parent application
          saisri2k2 Level 4

          To avoid these confusions and dependencies, you might have to implement your project using frameworks like Robotlegs, cairngorm, Mate.


          Normally I would do this in the application. Step 1. use singleton to hold your data (AC). Step 2: use the same singleton to get or insert the data. Step 3: use custom events X for the application to know that the data is inserted or updated. Step 4: throw a custom event X from the popup to let know the application(who is listening for this event X) that data is updated, Step 5: The application already has the reference to the singleton, so, it catches the X event and asks the singleton to update the Arraycollection(AC). Step 6: Bind the AC to the List

          • 2. Re: Need to refer back to the parent application
            Kristin95762 Level 1



            Thanks for taking the time to reply to my question.


            However, I'm still trying to wrap my head around Flex 4 and classes. All those other topics are sometime down the road.



            • 3. Re: Need to refer back to the parent application
              kokorito Level 4

              why dont you connect the titlewindow to the database class and the main to your database class

              whenever the titlewindow makes changes to the database class, the database class dispatches an event to say the data has changed

              main listens for this event so it knows when to update the display


              you are going to need one database object that is easily accesible to other classes (main and titlewondow). You dont want to instantiate a new object everytime you need to connect to the database class. You could instantiate it once and then pass it to every other class, but thats a pain.

              Better to have one instance thats globally accessible. A data Model, commonly a SIngleton class. (you guessed it was coming)

              • 4. Re: Need to refer back to the parent application
                Kristin95762 Level 1

                Oh god!


                I've been playing with this Flex thing for about 8 months and I'm self taught. I'm not a programmer. I had a class or 2 in Flash. Just basics.


                Singleton??? I just got the gist of a class (sort of). I have 2 simple classes that 1) allow me to load a list on startup, and 2) insert some data. That's it.


                Can't I just use FlexGlobals to update the dataprovider in my main class file?




                I mean, doing things the wrong way first usually helps me see the right way eventually.






                PS: how do you mean connect my main app and the title window to the main class? Yeah, yeah, yeah, I'm trying to run before I can crawl. Oh well.

                • 5. Re: Need to refer back to the parent application
                  kokorito Level 4

                  you have more than 2 classes. Everything is a class, main is a class, titlewindow is a class, a button is a class

                  but you dont directly use classes, you use instance or objects of the class when you use myDB = new DatabaseManager() the myDB is an instant of the class DatabaseManger


                  if in your Databasemanager you have methods insertSomething() and fetchTable()

                  part of the insertSomething method is a dispatchEvent


                  public function insertSomething(thing):void


                       //do something;

                       dispatchEvent(new Event("doneSomeInserting"));




                  so in your titlewindow you have

                  myDB = new Databasemanager();




                  now in your main you can listen for that event so when the data changes main knows when it has to refresh, something like

                  myDB = new DatabaseManager();


                  private function updateMe():void






                  so the titlewindow inserts to DatabaseManager, which then dispatches an event to broadcast things have changed. main listens for that event to know when it needs to update. Not just main, you can have other componets listen (subscribe) to the event. You might have another page with a graph.


                  But that doesnt work because you have created two myDB objects. The myDB in titlewindow is different to the myDB in main. They are two different instances. When the myDB in titlewindow dispatches an update event, main wont hear it as it is listening to the myDB created in main, a different myDB.


                  So what you need is one instance of the DatabaseManager class. You want titlewindow to use the same instance as the main uses. The simplest way to do that is with a Singleton. A Singleton just ensures there is only one istance of a class. So when titlewindow inserts something and invokes the event, main is listening to the same instance.


                  There are, of course, other ways to do it. But I find (at the moment) that the Singleton is the simplest. It actually makes things easier. Try it

                  • 6. Re: Need to refer back to the parent application
                    Amy Blankenship Level 4

                    I would not suggest that you couple your titlewindow to the database access Class.  If you need to display or edit different data from the same titlewindow, you would then have a big problem.  By the same token, if you want to edit the same data from a different View, you would have to repeat the database logic there.


                    The architecture you already have is much closer to what I would consider good, decoupled architecture.  Keep doing what you're doing.  Dispatch the Event from the titlewindow, then in the handlert in the main Class, simply have the main Class use the information it received in the Event to call whatever needs to be called on the database access class instance it has a reference to.


                    Something like


                    protected function onStuffEditDone(e:StuffEditEvent):void {

                         var stuff:Stuff = event.stuff;




                    I know you're not ready to get your head around Frameworks, but here is a great video that helps introduce you to the concepts used in Frameworks http://tv.adobe.com/watch/360flex-conference/mate-flex-framework-by-laura-arguello/ , so that you can start out building your Classes in such a way that when you are ready, your Classes won't need rework to incorporate the framework of your choice.  Keeping everything loosely coupled will make things easier to maintain, whether you are using a Framework or not.





                    • 7. Re: Need to refer back to the parent application
                      Don Kerr

                      It is prefectly fine to take advantage of FlexGlobals.topLevelApplication.


                      One of the powers of Flex is there are a millon ways to accomplish your goal.  All valid.  3rd party frameworks is one option some pursue, but not required.


                      You have to look at your goals, consider the options, and decide what works best for you.


                      I often put [Binable] var myDataProvider:ArrayCollection;

                      in the main application


                      Then use {FlexGlobals.topLevelApplication.myDataProvider} throughout the app wherever I need it.


                      Just choosing Flex is all the "Best Practice" you need.



                      1 person found this helpful
                      • 8. Re: Need to refer back to the parent application
                        Amy Blankenship Level 4

                        That depends on what you mean by "acceptable."  If your goal is a tightly coupled architecture that's hard to maintain, then yes, go for that approach.  If your application has the potential to need maintenance for longer than about 2 weeks, I would say that it is not.


                        Check out these resources for more info



                        1 person found this helpful
                        • 9. Re: Need to refer back to the parent application
                          Kristin95762 Level 1

                          Wow. You are super. What a great explanation.


                          I'm going to look into Singleton's a little more.


                          I did come to the conclusion that my use of two classes was the problem, and after I noodled through your previous response I did realize that I could use one class. I still had a problem updating my list box, though the data was getting to the dataprovider.


                          I ended up with this solution for now.


                          private function refreshListResult(event:SQLEvent):void


                          var data:Array = refreshAuthorList.getResult().data;

                          FlexGlobals.topLevelApplication.authorsList.dataProvider = new ArrayCollection(data);



                          But, I am going to look at this Singleton business. Because I have a whole lot of plans in mind.


                          I will study your reply carefully. I really appreciate your help.



                          • 10. Re: Need to refer back to the parent application
                            Kristin95762 Level 1

                            Thanks Amy. I'll check out the video.


                            I'm making headway slowly but surely. I have a couple of books and help from folks like you and regular aha moments when I put these resources together.


                            I can see the logic of your explanation. It looks rather like a pretzel, sort of looping back to the same point. Well, in my mind anyway.


                            Thanks bunches.



                            • 11. Re: Need to refer back to the parent application
                              Kristin95762 Level 1

                              Ah, so much great feedback.


                              Thank you for not making me feel like a cheater. I finally used this solution:


                              private function refreshListResult(event:SQLEvent):void


                              var data:Array = refreshAuthorList.getResult().data;

                              FlexGlobals.topLevelApplication.authorsList.dataProvider = new ArrayCollection(data);



                              But I'm in a learning process and it may not stay that way. I just like to get something working.


                              Bindings. I've used a couple because they're in the examples I've seen. I need to try to get a better handle on those.


                              Yeah, on all those millions of ways to do things. I'm sure all the variety is great. But when it comes to looking at other peoples examples it can be a really hard job making sense of all of it.





                              • 12. Re: Need to refer back to the parent application
                                Kristin95762 Level 1

                                Hi again.


                                Actually, I'm not looking for acceptable in the long run. It just helps me to make headway, seeing how things work, making connections, etc... Eventually I see a better route.


                                Thanks for your help. I appreciate it very much.


                                My husband thinks this is just an excercise in futility. But I'm very serious about it. I'm not interested in investing my time making doo doo.


                                Thanks again.



                                • 13. Re: Need to refer back to the parent application
                                  Amy Blankenship Level 4

                                  I believe Kokorito was suggesting not that you use a Singleton (a global state variable that will bite you in the end http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/), but a singleton (you simply only ever instantiate one instance, as with Robotlegs' injector.mapSingleton.  At least I hope so.  You have a lot of promise.  I'd hate to see you go off into the weeds.

                                  • 14. Re: Need to refer back to the parent application
                                    Kristin95762 Level 1

                                    Okay, this sounds like one of those things about Flex that makes me pull my hair out.


                                    You mean there's a difference between 'Singleton' and 'singleton'?




                                    You may wonder why I'm jumping into a project that I seem to have little knowledge about.


                                    Years ago (!) I studied some COBOL in college. After that I played with Visual Basic. Then Flash. I've never been a gainfully employed programmer. I was a secretary and then a stay-at-home mom. But I've always wanted to program something. The problem was that I just didn't know what to program. And frankly, VB is visually boring. I loved those widgets that added little animations though. That was absolutely the most amazing thing I'd ever seen. Because until that time, animation was very limited. Adobe invested time and effort to make a suite of products that brings all these great tools together, and then someone like me can actually create something at home that others might find useful. Then one day it finally struck me what I wanted to create and I finally got a fire lit under my butt.


                                    So, I'm finally doing something I've always wanted to do, but it's like jumping into the deep end of the pool without a lifejacket. To hell with starting from the beginning, right?



                                    • 15. Re: Need to refer back to the parent application
                                      Don Kerr Level 3

                                      One of the best resources for "simple", "GitRdone" examples is  Tour De Flex http://www.adobe.com/devnet-apps/flex/tourdeflex/web/


                                      There are some good examples about binding there.



                                      • 16. Re: Need to refer back to the parent application
                                        Amy Blankenship Level 4

                                        Hey, Kristin;


                                        The difference between a Singleton and a singleton is that a key part of the Singleton pattern is that the Class enforces one instance (which is not so bad), and then it provides a handle to that instance to anyone that asks for it (which is horrible, and that's the same thing that's wrong with using FlexGlobals).  I provided you with links to learn about all the many things that are wrong with doing this, but the thing that will concern you over the long haul is that literally ANYTHING in the program could be changing or accessing a global variable, and there is no way to know what caused a problem without reading the code of EVERY Class that has a reference to it.  And even then, you might not be able to track it down if your project is large enough  (I've been there!).


                                        This problem is even worse with topLevelGlobals, because your references to it won't have a direct F3 link to the place the variable is defined.


                                        If you are using singleton with a lower case s, you are simply making sure there is only one instance (by only creating one--if you are sparing with the new operator, this is easily doable).  Then you provide a reference to that single instance through normal means, such as setting a property on a component that needs an instance of that Class.


                                        So, for instance, your main application could simply create an instance of your database access Class.  Your TitleWindow could have a property on it that you then pass that instance into, and perform all the database access stuff.  However, good architecture dictates that your View should only display data and allow users to interact with that data, not decide what happens as a result of that interaction (this is business logic, which is not the responsibility of the View).  So it is better to bubble the event out of the View and let it be handled further up.


                                        I know you are just starting out and you think it is unique to be self-taught, but you'd be surprised how many people here are also self-taught (myself included).  IMO, it is better to learn good habits from the start, and I think it's worth the effort.  If you think you want to do this for a living, having bad habits can keep you from progressing to being an architect (http://sites.google.com/site/steveyegge2/singleton-considered-stupid).


                                        The reason Singleton is popular with so many otherwise talented developers is that when ActionScript 3 came out, there was a big flap about how the lack of private constructors prevented a "true" Singleton pattern implementation (this was before even companies liike Google realized how destructive this "pattern" is). It became a mark of your as3 programming know-how that you knew how to make one that could enforce its single instance.  Many programmers picked up on that, but missed the subsequent discussions about how even the Gang of Four, who wrote "Design Patterns: Elements of Reusable Object-Oriented Software," wished later that they had never included it in the book.


                                        I think you have it in you to be truly extraordinary, and in my own little way, I'm encouraging you to follow that path, to be like these amazing ladies: http://www.xxcoder.net/ & http://queencodemonkey.com/ .  The type of developer you want to be is up to you in the end, though.

                                        • 17. Re: Need to refer back to the parent application
                                          Kristin95762 Level 1

                                          Hi Amy;


                                          Thank you for your thoughtful reply. I will take time to study and absorb that commentary.


                                          I have absolutely no doubt that there are many people, even women like me (yourself too) that enjoy the rewards of programming and designing applications. That's just the point I was making about Adobe. That I am so thankful they have invested in these great technologies that everyone can have access to. And I am thankful there are women such as yourself blazing the way.


                                          I will survey all the links you have forwarded.


                                          Thanks again.



                                          • 18. Re: Need to refer back to the parent application
                                            Kristin95762 Level 1

                                            Hi Amy,


                                            I started to read the 'stupid' singleton link you sent, and before I got to the bottom I thought, "this sounds like COBOL type procedural stuff'. Low and behold, I get to the bottom and I was right.


                                            Sounds like I know what a Singleton is after all.