Select to view content in your preferred language

Use of variable outside queryFeatures function

454
5
04-23-2014 10:47 AM
RyanSmith1
New Contributor III
Hi
I have the following code to query different feature layers. I would like to know if it's possible to use the searchItems variable outsidethe actual queryFeatures anonymous function because i need to get all the results from the different layers and then attach it to one datagrid.

Thanks 


        layers[layerId].LayerVariable.queryFeatures(query, function (featureSet) {
            resultFeatures = featureSet.features;
                           

                     var items = dojo.map(featureSet.features, function (feature) {
                        return feature.attributes;
                     });

                    
                         var data = {
                             identifier: "OBJECTID",
                             items: items
                         };

                         node = dojo.byId("searchHeader");

                         var store = new dojo.data.ItemFileReadStore({
                             data: data
                         });


                         grid = new dojox.grid.DataGrid({
                             store: store,
                             structure: structure
                         });


                         node.appendChild(grid.domNode);

                         grid.startup();
                     }
                }    
     
        });

0 Kudos
5 Replies
SteveCole
Frequent Contributor
You could have a globally declared array and every time the queryFeatures function runs, just push the features it returns into the array. Something like..
var allFeatures = {};

layers[layerId].LayerVariable.queryFeatures(query, function (featureSet) {
     dojo.forEach(featureSet.features, function(feature) {
          allFeatures.push(feature);
     });
     .
     .
     .
}


Steve
0 Kudos
RyanSmith1
New Contributor III
You could have a globally declared array and every time the queryFeatures function runs, just push the features it returns into the array. Something like..
var allFeatures = {};

layers[layerId].LayerVariable.queryFeatures(query, function (featureSet) {
     dojo.forEach(featureSet.features, function(feature) {
          allFeatures.push(feature);
     });
     .
     .
     .
}


Steve




Hi Steve thanks for the response. I've tried that approach but the allFeatures array becomes empty after the function is completed.
0 Kudos
JohnGravois
Frequent Contributor
if you'd like to create an empty array object, you should be using brackets.  curly braces are reserved for object literals.

var allFeatures = [];
//not
var allFeatures = {};


not sure if that explains why what you have is empty later though..
0 Kudos
RyanSmith1
New Contributor III
if you'd like to create an empty array object, you should be using brackets.  curly braces are reserved for object literals.

var allFeatures = [];
//not
var allFeatures = {};


not sure if that explains why what you have is empty later though..



Thanks Steve, John

Turns out the global array is holding the items but code after the queryFeatures function is being executed before the actual function. Any ideas as to how i can solve this issue?
0 Kudos
TracySchloss
Frequent Contributor
You need to look through the threads searching for queryTasks and deferred.   When you execute multiple queries, you have to wait for all queries to finish before you proceed. ( In AMD-style you're searching for promise/all instead.)  It can be rather complicated to wrap your head around, but its the way you have to set it up.
0 Kudos