Trouble grasping concept of inner or nested functions - problems with IE8?

430
0
11-15-2012 06:00 AM
TracySchloss
Frequent Contributor
Working through a variation of the find nearby mobile example, I am running a selectFeatures and creating a list of the closest sites to an address. The values are first sorted by distance, and the list contains the name of the facility, it's address and the distance from the starting address. Last, there is a button that will show a map with the sites, draw the routes and give the user a chance to get directions.

It's the definition of the locationDetails that's giving me fits. It's works fine in Firefox, but in IE8, it never seems to execute the function that supposed to generate the routes etc. The lines I've changed to red are the problem area. I'm relatively new to Javascript and I'm having a hard time grasping the concepts of nested or inner functions. Something I read made me think that IE 8 handled these differently and that maybe I needed to modify my code? My problem is I don't know what to modify it to. All I can see is that it never executes the function mapResults.
                dojo.connect(featureLayer, "onSelectionComplete", function(features){
                    itemList = dijit.byId('searchResults');  //changed this to be global 11/9/12
                    //Remove any existing items from the result window
                    while (itemList.domNode.hasChildNodes()) {//may want to move this sooner, user can see the old list for a moment
                        itemList.domNode.removeChild(itemList.domNode.lastChild);
                    }
                    //If no results are found users should increase search distance
                    if(features.length === 0){
                            var noResults = new dojox.mobile.ListItem({
                            label: "No Results"
                        });
                        noResults.set("class", "mblVariableHeight");
                        noResults.domNode.innerHTML = "No results found.  Try a larger search distance using the settings option.";
                        itemList.addChild(noResults);
                      //  return;
                    }
                    //modifies the returned distance to something more user friendly
                    dojo.forEach(features, function (feature) {
                        feature.distance = Math.round((esri.geometry.getLength(feature.geometry, currentGraphic.geometry) / 1609.344) * 100) / 100;
                    });
                    
                    //sorts the returned features so closest features are at the top
                    var sortedFeatures = features.sort(function(a, b){
                        return a.distance - b.distance;
                    });
        
                    dojo.forEach(sortedFeatures, function (result) {       
                        var resultItem = new dojox.mobile.ListItem({ label: result.attributes.FACILITY });
                        resultItem.set("class", "mblVariableHeight");
                        var content = [];
                        content.push(result.attributes.FACILITY + "<br />" + result.attributes.ADDRESS + "<br />" );                    
                        content.push(result.distance + " miles<br/>");
                        var formatContent = content.join("");        
                        resultItem.domNode.innerHTML = formatContent;
                        var button = new dojox.mobile.Button({
                            label: 'Map It',
                            moveTo: 'mapView'
                        }).placeAt(resultItem.domNode);
        
                        var locationDetails = function (result) {
                             return function (results) {  //(sortedFeatures);   ??                   
                                mapResults(results, result); //mapResults(sortedFeatures,result);??
                            };
                        };        
                        dojo.connect(button, "onClick", locationDetails(result));
                        //clearProgress();
                        itemList.addChild(resultItem);
                    });
                    clearProgress();
                    //
                });


Function that is supposed to add functionality to the button
//for routing
function mapResults(results, feature) {
console.log("mapResults function");
//move to the map view to display the route
var widget = dijit.byId('resultsView');
widget.performTransition('mapView', 1, "slide", function () {
map.resize();
resultLocationsLayer.clear();
routeGraphicLayer.clear();
segmentGraphicsLayer.clear();

//Define the route input parameters
params.directionsLengthUnits = esri.Units.MILES;
params.outSpatialReference = map.spatialReference;
params.stops.features = [];

//Add the starting location to the map
var fromSymbol = new esri.symbol.SimpleMarkerSymbol().setColor(new dojo.Color([105, 153, 0]));
var startLoc;
if (pointSource == "geoLocation") {
startLoc = new esri.Graphic(esri.geometry.geographicToWebMercator(new esri.geometry.Point(currentLocation, new esri.SpatialReference({
wkid: 4326
}))), fromSymbol, {
Name: dojo.byId('loc').textContent,
RouteName: feature.attributes.FACILITY
});
console.log("using geoLocation for start point");
} else {
startLoc = new esri.Graphic((new esri.geometry.Point(currentLocation, new esri.SpatialReference({
wkid: 4326
}))), fromSymbol, {
Name: dojo.byId('loc').textContent,
RouteName: feature.attributes.FACILITY
});
console.log("using address for start point");
}
resultLocationsLayer.add(startLoc);
params.stops.features[0] = startLoc;

//Add the ending location to the map
var toSymbol = new esri.symbol.SimpleMarkerSymbol().setColor(new dojo.Color([204, 0, 0]));
var endLoc = new esri.Graphic(feature.geometry, toSymbol, {
Name: feature.attributes.FACILITY,
RouteName: feature.attributes.FACILITY
});
resultLocationsLayer.add(endLoc);
params.stops.features[1] = endLoc;

//Get the Route and display it and the directions on the MapView
routeTask.solve(params, function (solveResult) {
var directions = solveResult.routeResults[0].directions;
directionFeatures = directions.features;
var content = [];
content.push("<ol>");
routeGraphicLayer.add(new esri.Graphic(directions.mergedGeometry));
dojo.forEach(directions.features, function (feature, index) {
if (index === 0 || index === directions.features.length - 1) {
content.push("<li>" + feature.attributes.text + "</li>");
} else {
content.push("<li onclick='zoomToSegment(" + index + "); return false;' class=\"segment\"><a href=\"#\">" + feature.attributes.text + "</a></li>");
}
});
content.push("</ol>");
dojo.byId("directions").innerHTML = content.join("");
map.setExtent(directions.extent, true);
}, function (error) {
alert('Sorry, could not create route.');
dojo.byId("directions").innerHTML = "<b>Error:</b> ";
});
});
}
0 Kudos
0 Replies