2 Replies Latest reply on Feb 21, 2009 10:04 AM by lukemack

    Including actionscript in a flex application

    lukemack Level 1
      If I include a an external actionscript file in a flex mxml file, I get different behaviours at compile time depending on the method used. Using an mx script tag with a xource attribute or an include statement, then compiling the file gives errors like:

      Error: Packages cannot be nested.

      If use import these errors go away and the file compiles but then I have problems when trying to instantiate the class.

      import lib.journal;
      public var testing:journal = new journal();
      testing.init();

      which gives:

      Error: Access of undefined property testing.

      Can anyone explain what is going on here? What effect does including the file as opposed to importing it have on packages and scope?

      thanks,
        • 1. Including actionscript in a flex application
          IanPMcC
          Including the file is equivalent to "cut and paste the contents of the file here," so you're getting the "Packages cannot be nested" because presumably the include statement appears within an implied package scope (your MXML component.)

          As for the import case, I'm not entirely sure about the form of your code, but generally, you want to import into the package scope, and then you want to use the instances from within a function scope. An mx:Script block is essentially code that's inserted into the class, so I think the problem there is that you're calling testing.init() from "nowhere." The "public var testing:journal = new journal();" snippet creates a new property on your class called "testing," but the subsequent init() call is free floating in the class definition and not in any function context, which I think is what's causing your problem.

          I'd recommend trying putting your attempted use of the class into a function, and calling it, perhaps like the attached code.

          HTH,
          Ian
          • 2. Re: Including actionscript in a flex application
            lukemack Level 1
            thanks. i think that makes sense. I'm now getting another error though:

            journal/trunk/lib/MindJournal.as(42): col: 41 Error: Type was not found or was not a compile-time constant: SQLEvent.

            private function selectResult(event:SQLEvent):void {
            ^
            journal/trunk/lib/MindJournal.as(65): col: 40 Error: Type was not found or was not a compile-time constant: SQLErrorEvent.

            private function selectError(event:SQLErrorEvent):void {

            I'm trying to adapt what is here:

            http://www.adobe.com/devnet/air/flex/quickstart/simple_sql_database.html

            to use an external actionscript class which now looks like:

            [code]
            package lib {

            import flash.data.SQLResult;
            import flash.data.SQLConnection;
            import flash.filesystem.File;
            import flash.data.SQLStatement;
            import flash.data.SQLConnection;

            public class MindJournal {

            private var conn:SQLConnection;
            private var createStmt:SQLStatement;
            private var insertStmt:SQLStatement;
            private var insertStmt2:SQLStatement;
            private var selectStmt:SQLStatement;

            public function init() {

            conn = new SQLConnection();
            conn.addEventListener(SQLEvent.OPEN, openSuccess);
            conn.addEventListener(SQLErrorEvent.ERROR, openFailure);
            var dbFile:File = File.applicationStorageDirectory.resolvePath("/db/journal.sqlite");
            conn.openAsync(dbFile);
            //getData();
            }


            private function getData():void {
            status = "Loading data";

            selectStmt = new SQLStatement();
            selectStmt.sqlConnection = conn;
            var sql:String = "SELECT id, column_1, column_2 FROM entries";
            selectStmt.text = sql;

            selectStmt.addEventListener(SQLEvent.RESULT, selectResult);
            selectStmt.addEventListener(SQLErrorEvent.ERROR, selectError);

            selectStmt.execute();
            }

            private function selectResult(event:SQLEvent):void {

            status = "Data loaded";

            selectStmt.removeEventListener(SQLEvent.RESULT, selectResult);
            selectStmt.removeEventListener(SQLErrorEvent.ERROR, selectError);

            var result:SQLResult = selectStmt.getResult();

            var numRows:int = result.data.length;
            for (var i:int = 0; i < numRows; i++)
            {
            var output:String = "";
            for (var prop:String in result.data )
            {
            output += prop + ": " + result.data
            [prop] + "; ";
            }
            trace("row[" + i.toString() + "]\t", output);
            }
            resultsGrid.dataProvider = result.data;

            }

            private function selectError(event:SQLErrorEvent):void {

            status = "Error loading data";

            selectStmt.removeEventListener(SQLEvent.RESULT, selectResult);
            selectStmt.removeEventListener(SQLErrorEvent.ERROR, selectError);

            trace("SELECT error:", event.error);
            trace("event.error.message:", event.error.message);
            trace("event.error.details:", event.error.details);
            }


            } //end of class
            } //end of package



            [/code]