4 Replies Latest reply on Mar 30, 2011 8:35 PM by cut-copy-paste

    How to get data from Sqlite/actionscript model to components..?

    cut-copy-paste Level 1

      I'm trying to get my Actionscript 3.0 model that links to an SQLite database using Paul Robertson's SQLRunner class to talk to my flex component; I'm really unsure of how best to accomplish this. I have worked off a few examples, but I don't know the simplest way to tell my component the results of the SQL query. Anyone have any recommendations?  I have looked a bit at the robotlogs framework as I have a sample app, but I don't really understand how things are going back and forth, and I'd like something that is fairly straightforward.. not necessarily totally decoupled.


      Here is some of the code, to give you an idea of what I'm working with right now.


      <model:Patient id="editedPatient" FirstName="{FirstName.text}" />

              protected var _patient:Patient;

              public function get patient():Patient
                  return _patient;

              public function set patient(value:Patient):void
                  _patient = value;

              private function creationCompleteHandler(event:FlexEvent):void{



              protected function save_clickHandler(event:MouseEvent):void


      <s:TextInput id="FirstName" text="{patient.FirstName}" />
      <s:Button id="save" label="save" click="save_clickHandler(event)" />


          public function getPatient(PatientId:int):void {
      var stmt:String = new String();

      = "SELECT * FROM Patient WHERE PatientID= @PatiendId;";
      .execute(stmt, {PatientId:PatientId}, loadPatient_result, Patient);


      private function loadPatient_result(result:SQLResult):void
      if (result.data != null && result.data.length > 0)
      var Patient:Patient = result.data[0];

        • 1. Re: How to get data from Sqlite/actionscript model to components..?

          From the basic sample you've shown, I think you've got all the SQLite part written the right way, and your component looks straightforward. It sounds like the real question you're asking is more about "once I've loaded some data in my app, how do I get it to show up on the screen?" Or put in a more generalized way, "how do I get different pieces of my code to talk to each other?"


          Interestingly, that's exactly the question that Robotlegs and similar frameworks (aka "micro-architectures") try to answer. So one answer to your question would be to use something like Robotlegs for your inter-app communication. Most of the current generation of micro-architecture frameworks are built around a couple of core principles:

          - Automated dependency injection: you don't have to figure out how to get the "model" instance created and passed to the view -- the framework does that for you

          - Message passing: Different parts of the app don't call methods or set properties on each other directly -- instead they use events (or some similar mechanism) to trigger method calling or property setting. (This makes the code more "decoupled" as you mention, which many people consider to be a benefit, but it does make it harder to follow until you're used to that style of organizing code.)



          If you don't want to go to the level of using a framework, then you're back to traditional ways of creating objects and accessing them. So if your component needs access to a Patient object, these would be the traditional ways of giving it the Patient object:

          1. The component creates the object itself

          2, Some other object that knows about your component (probably the parent of your component) passes the Patient object in by calling a method or setting a property

          3. You use some globally available mechanism for getting the Patient object (usually a static method or property)



          I personally have been through the various "traditional" methods and I've moved on to using Robotlegs, because I think it provides useful guidance and reduces the amount of "plumbing" code I have to write. I won't say it didn't take some effort to learn, though I'm glad I put in the effort.


          If you're interested in thoughts on how I would accomplish this with Robotlegs (or with any of the other mechanisms I mentioned) let me know what looks promising to you and I'll be happy to give more detail.

          1 person found this helpful
          • 2. Re: How to get data from Sqlite/actionscript model to components..?

            I stumbled on the exact same problem a while ago, and got saved by a very interesting series of tutorials on Cairngorm (ultra-scary for noobs like myself) by a guru named David Tucker - and by applying the IModelLocator pattern (applying the insight gained on the the 1st and second parts of the aforementioned series - before the "really scary stuff" begins to creep in) saved my day - in less than 1 hour - I kind of "saw the Light" - hope it does the same for you:





            • 3. Re: How to get data from Sqlite/actionscript model to components..?
              cut-copy-paste Level 1

              Thanks for the reply




              I have ended up with something that seems to be working finally.. it's very inspired by robotlegs probably.. I found the SQLite robot-legs-addressbook example extremely helpful and borrowed large swaths of code from it; it was really tricky for me to figure exactly what was doing what (especially because I wasn't aware yet of some of the finer points of debug mode and that thing where you cmd+click and it goes right to the function definition)..  I got it working by adding handlers as arguments to the add/update/modify functions..


              in the model:


              private var _doneAddHandler:Function;



              public function add(doneAddHandler:Function):void {

              var stmt:String = new String();


              _doneAddHandler = doneAddHandler;






              private function add_result(results:Vector.<SQLResult>):void






              then in the component:


              public function loadExercise():void {

              _exercise.loadExercise(currentUser, DateTime, doneLoadExercise);




              That is allowing me to grab the info I need from the database and dump it into the component and back and forth. Probably a little less decoupled than robotlegs.. which I was looking at a bit based on its use in SQLRunner and addressbook, and was quite tempted, but I was on a tight timeline and it seemed very daunting up front; I didn't find a strong enough handhold on it, even though on paper it seemed like exactly the solution to my problems.

              I will have to write model classes for some specific views to get the basic information out, but now that communication's going on everything seems a lot more attainable.

              • 4. Re: How to get data from Sqlite/actionscript model to components..?
                cut-copy-paste Level 1

                I can most definitely relate to the 'ultra-scary for noobs like myself' sentiment. Aiee!


                I think I'm too far along to dive into a framework at this point, but if I'm back in Flex again I will strongly consider it now that I'm a bit less green.  I will check that out regardless, though, it could well have some good stuff in there.