tags:

views:

3940

answers:

3

I have a datagrid defined in an mxml file (flex 3):

I am using an external class to connect to a sqlite database and generate some results (this is working and I can trace the results).

How can I target the datagrid generated in the mxml from the external class? I have tried:

Application.application.resultsGrid.dataProvider = results.data;

And get 'Error: Access of undefined property Application.' from the amxmlc compiler.

I've also tried:

[Bindable]
public var resultsGrid:DataGrid;

In the class properties.

A: 

Update: The phrasing of your question confused me :(

If you need to populate the datagrid with from you db, you really should be looking at binding the dataProvider property.

dirkgently
+1  A: 

Looks like I needed to include import mx.core.*; and it now works.

I don't really understand your answer. Am I not binding the dataprovider property by doing:

Application.application.resultsGrid.dataProvider = result.data; ?

I'm from a PHP background and familiar with OOP in that environment so the idioms in Flex are quite strange to me.

codecowboy
That's not binding, it's assignment. Info on binding:http://www.adobe.com/devnet/flex/quickstart/using_data_binding/
cliff.meyers
A: 

as brd664 says, what you are actually doing in

Application.application.resultsGrid.dataProvider = result.data;

is actually an assignment. It's just like assigning a value to variable as in

var a : uint = 1;

Binding gives you a little more structure and allows you to populate multiple components based on a single property update. There's a ton of other benefits from binding and probably too much to cover in this post.

Here is a quick and simple example of how binding works. Note that there is one property that is bindable... when you click the button it sets that property to the value of whatever is in the textInput. This update then causes the bindings to fire and updates anything that has been bound to that property. It's one of flex's biggest features (it's also used extensively in silverlight and wpf and probably a load of other technologies that i'm not aware of). Anyway... have a play with it and see if you can get your component to update from a binding.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">

    <mx:Script>
        <![CDATA[

            private var _myData : String

            [Bindable]
            public function get myData() : String
            {
             return _myData;
            }
            public function set myData(value : String) : void
            {
             _myData = value;
            }

            private function clickHandler(event : MouseEvent) : void
            {
             myData = myTextInput.text;
            }
        ]]>
    </mx:Script>
    <mx:VBox>
     <mx:HBox>
      <mx:Label text="{myData}" />
      <mx:Label text="{myData}" />
      <mx:Label text="{myData}" />
     </mx:HBox>
     <mx:TextInput id="myTextInput" text="TYPE HERE" />
     <mx:Button label="CLICK TO BIND" click="clickHandler(event)" />
    </mx:VBox>

</mx:Application>
James Hay