Select to view content in your preferred language

How to bind object instances created in Actiom script to properties in mxml?

685
2
Jump to solution
07-12-2012 11:28 AM
ionarawilson1
Deactivated User
How can we reference an object instance created with ActionScript in mxml?

So let's say I create a function that creates a query task and a query. I want the results of this Query to populate a datagrid that has been created in mxml. If I set the dataProvider of the datagrid to   dataProvider="{querytaskindustry.executeLastResult.attributes}"

in mxml I get a warning saying that the binding will not occur because it can't identify querytask. I believe this happens because I did not set the variable for the querytask to be bindable. However I can't set a variable to be bindable inside a function. If I create the querytask outside the function and set it as bindable it does not work because the function is based on a button click so the query is just created when the user clicks the search button, so I need to create the querytask and query inside the function. What would be the solution for the mxml datagrid dataProvider property identify the querytask? Thank you!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarkHoyland
Frequent Contributor
You can still instantiate the Bindable query task outside of the function.
You just set its paramaters when the button is clicked (or when you need to execute the querytask.)

This code is an adaptation of the ESRI sample at: http://help.arcgis.com/en/webapi/flex/samples/index.html#/Query_result_in_table/01nq0000005t000000/

I have just commented out the MXML query and query task, and created and used them in actionscript.


<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"       xmlns:mx="library://ns.adobe.com/flex/mx"       xmlns:esri="http://www.esri.com/2008/ags"       xmlns:s="library://ns.adobe.com/flex/spark"       pageTitle="Query Task (without a map)">  <!--  This sample shows how to query the server and displaying  the result in a datagrid.  The query is sent using the execute() method on a QueryTask.    This sample sets up a QueryTask (what layer on what server to query).  When the user clicks the "Get Details" button, a Query is sent with  the user-provided text to search for.  Meanwhile a DataGrid has been created which listens for the results  (using executeLastResult) from the querytask.  -->    <s:layout>   <s:VerticalLayout horizontalAlign="center" paddingTop="25"/>  </s:layout>    <fx:Script>   <![CDATA[    import com.esri.ags.tasks.QueryTask;    import com.esri.ags.tasks.supportClasses.Query;        [Bindable]    public var queryTask:QueryTask;        protected function button1_clickHandler(event:MouseEvent):void    {     // Set up the query     var query:Query = new Query;     query.outFields = ["STATE_NAME","STATE_FIPS","SUB_REGION","STATE_ABBR","POP2000","POP2007"]     query.returnGeometry = false;     query.text = stateName.text;          //Use the bindable querytask     queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5")     queryTask.useAMF = false;     queryTask.execute(query);    }   ]]>  </fx:Script>      <fx:Declarations>   <!--<esri:QueryTask id="queryTask"       url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"       useAMF="false"/>   <esri:Query id="query"      outFields="[STATE_NAME,STATE_FIPS,SUB_REGION,STATE_ABBR,POP2000,POP2007]"      returnGeometry="false"      text="{stateName.text}"/>-->  </fx:Declarations>    <s:Panel title="Using Query tasks without maps">   <s:layout>    <s:VerticalLayout/>   </s:layout>   <s:HGroup verticalAlign="middle">    <s:Label text="US state name: "/>    <s:TextInput id="stateName" text="Carolina"/>    <s:Button click="button1_clickHandler(event)" label="Get Details"/>   </s:HGroup>   <mx:DataGrid id="resultsGrid"       dataProvider="{queryTask.executeLastResult.attributes}"       visible="{queryTask.executeLastResult != null}">    <mx:columns>     <mx:DataGridColumn dataField="STATE_NAME" headerText="State Name"/>     <mx:DataGridColumn dataField="SUB_REGION" headerText="Region"/>     <mx:DataGridColumn dataField="STATE_FIPS" headerText="FIPS"/>     <mx:DataGridColumn dataField="STATE_ABBR" headerText="Abbreviation"/>     <mx:DataGridColumn dataField="POP2000" headerText="Population 2000"/>     <mx:DataGridColumn dataField="POP2007" headerText="Population 2007"/>    </mx:columns>   </mx:DataGrid>  </s:Panel> </s:Application>

View solution in original post

0 Kudos
2 Replies
MarkHoyland
Frequent Contributor
You can still instantiate the Bindable query task outside of the function.
You just set its paramaters when the button is clicked (or when you need to execute the querytask.)

This code is an adaptation of the ESRI sample at: http://help.arcgis.com/en/webapi/flex/samples/index.html#/Query_result_in_table/01nq0000005t000000/

I have just commented out the MXML query and query task, and created and used them in actionscript.


<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"       xmlns:mx="library://ns.adobe.com/flex/mx"       xmlns:esri="http://www.esri.com/2008/ags"       xmlns:s="library://ns.adobe.com/flex/spark"       pageTitle="Query Task (without a map)">  <!--  This sample shows how to query the server and displaying  the result in a datagrid.  The query is sent using the execute() method on a QueryTask.    This sample sets up a QueryTask (what layer on what server to query).  When the user clicks the "Get Details" button, a Query is sent with  the user-provided text to search for.  Meanwhile a DataGrid has been created which listens for the results  (using executeLastResult) from the querytask.  -->    <s:layout>   <s:VerticalLayout horizontalAlign="center" paddingTop="25"/>  </s:layout>    <fx:Script>   <![CDATA[    import com.esri.ags.tasks.QueryTask;    import com.esri.ags.tasks.supportClasses.Query;        [Bindable]    public var queryTask:QueryTask;        protected function button1_clickHandler(event:MouseEvent):void    {     // Set up the query     var query:Query = new Query;     query.outFields = ["STATE_NAME","STATE_FIPS","SUB_REGION","STATE_ABBR","POP2000","POP2007"]     query.returnGeometry = false;     query.text = stateName.text;          //Use the bindable querytask     queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5")     queryTask.useAMF = false;     queryTask.execute(query);    }   ]]>  </fx:Script>      <fx:Declarations>   <!--<esri:QueryTask id="queryTask"       url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"       useAMF="false"/>   <esri:Query id="query"      outFields="[STATE_NAME,STATE_FIPS,SUB_REGION,STATE_ABBR,POP2000,POP2007]"      returnGeometry="false"      text="{stateName.text}"/>-->  </fx:Declarations>    <s:Panel title="Using Query tasks without maps">   <s:layout>    <s:VerticalLayout/>   </s:layout>   <s:HGroup verticalAlign="middle">    <s:Label text="US state name: "/>    <s:TextInput id="stateName" text="Carolina"/>    <s:Button click="button1_clickHandler(event)" label="Get Details"/>   </s:HGroup>   <mx:DataGrid id="resultsGrid"       dataProvider="{queryTask.executeLastResult.attributes}"       visible="{queryTask.executeLastResult != null}">    <mx:columns>     <mx:DataGridColumn dataField="STATE_NAME" headerText="State Name"/>     <mx:DataGridColumn dataField="SUB_REGION" headerText="Region"/>     <mx:DataGridColumn dataField="STATE_FIPS" headerText="FIPS"/>     <mx:DataGridColumn dataField="STATE_ABBR" headerText="Abbreviation"/>     <mx:DataGridColumn dataField="POP2000" headerText="Population 2000"/>     <mx:DataGridColumn dataField="POP2007" headerText="Population 2007"/>    </mx:columns>   </mx:DataGrid>  </s:Panel> </s:Application>
0 Kudos
ionarawilson1
Deactivated User
Oh, I see! I guess the function was not working because of another issue and not because I was declaring the queryTask outside the function! Thank you for sending me the example! This is very helpful!
0 Kudos