layer onclick event only triggered sometimes

1481
1
12-07-2014 10:16 PM
TukangSampah
New Contributor II

Greetings,

I have jSAPI 3.11 map page containing a layer that loads a point feature class which has a relationship defined with another feature class as the origin. My objective is to perform a relationship query when a point feature is clicked so that the fields from the origin feature class can be display in the content of an InfoTemplate. Here is a snippet of my code:

// init code here

map.on('load', function() {
    var attributes_0000;
    var relQuery_0000 = new RelationshipQuery();

    var regency = new FeatureLayer(
        'http://server/arcgis/rest/services/mymap/MapServer/99',
        {
            mode:FeatureLayer.MODE_ONDEMAND,
            outFields: ['*'],
            displayOnPan:false
        }
    );
    
    var infoTemplate_0000 = new InfoTemplate('Information', '');
    
    layer_0000 = new FeatureLayer(
        'http://server/arcgis/rest/services/mymap/MapServer/3',
        {
            mode:FeatureLayer.MODE_ONDEMAND,
            outFields:['*'],
            displayOnPan:false,
            infoTemplate: infoTemplate_0000,
            id:'LAYER 0000'
        }
    );

    // layer_0000.on('click', function(evt) {
    dojo.connect(layer_0000, 'onClick', function(evt){
        console.log('layer clicked!');
        attributes_0000 = evt.graphic.attributes;
        relQuery_0000.outFields = ['*'];
        relQuery_0000.relationshipId = 0;
        relQuery_0000.objectIds = [attributes_0000.OBJECTID];
        
        layer_0000.queryRelatedFeatures(relQuery_0000, function(result){
            var result1 = result[attributes_0000.OBJECTID];
            var result_feature = result1.features[0];
            
            var relQueryProv0000 = new RelationshipQuery();
            relQueryProv0000.outFields = ['*'];
            relQueryProv0000.relationshipId = 11;
            relQueryProv0000.objectIds = [result_feature.attributes.OBJECTID];
            
            regency.queryRelatedFeatures(relQueryProv0000, function(resultprov){
                var resultprov1 = resultprov[result_feature.attributes.OBJECTID];
                var result_feature_prov = resultprov1.features[0];      
                var content = '<table><tr><td>ATTR1</td><td> : </td> <td> '+attributes_0000.ATTR1+'</td></tr><tr><td>ATTR2</td><td> : </td> <td> '+attributes_0000.ATTR2+'</td></tr><tr><td>ATTR3</td><td> : </td> <td> '+attributes_0000.ATTR3+'</td></tr><tr><td>ATTR4</td><td> : </td><td> '+result_feature.attributes.ATTR4+'</td></tr><td>ATTR5</td><td> : </td><td> '+result_feature_prov.attributes.ATTR5+'</td></tr><tr><td colspan="3" style="text-align: right;"><a class="showPopupDetails" href="javascript:void(0);" data-id="' + attributes_0000.OBJECTID + '" data-fclass="featureclass" data-layerindex="0000" data-ATTR4="'+result_feature.attributes.ATTR4+'" data-ATTR5="'+result_feature_prov.attributes.ATTR5+'" data-hasattachment = true >show details</a></td></tr></table>';
                //infoTemplate_0000.setTitle('Information');
                infoTemplate_0000.setContent(content);
                app.map.infoWindow.resize(300, 200);
            }); // province relationshipquery
        }); // regency relationshipquery
    }); // layer onclick
}); // map onload

As you can see from the code above there are two relationship queries performed (point feature class => regency feature class => province feature class). I can get the result from both relationship queries just fine.

My problem is: when I click on a point feature, the associated onclick event does not trigger consistently after the map page is refreshed. Sometimes the relationship queries execute and the results are shown in the InfoWindow as intended. However more often only an empty InfoWindow is shown (I don't get the "Layer clicked!" on line 31 in the browser's javascript console). I can also confirm that on Chrome 40 if I load the map in a new incognito window the onclick event triggers consistently, even between page refresh!

I would greatly appreciate if anyone has any idea what's going on with my code.

Regards,

Tukang

0 Kudos
1 Reply
HeikoHeijenga
Esri Contributor

By assigning an infoTemplate to layer_0000 you automatically set up a 'click' handler on the map, you don't have to do anything else to get 'default' popup behavior.

Seems to me that you want to show a custom popup, not one driven by the attributes of clicked feature.

So instead I would suggest to remove the infotemplate from layer_0000 and instead on line 51 do:

map.infoWindow.setContent(content);
map.infoWindow.show(evt.screenPoint, map.getInfoWindowAnchor(evt.screenPoint));
0 Kudos