Target must be an event emitter

4012
11
04-15-2014 10:23 AM
MattTenold
New Contributor III
I am currently using the Dojo/on and I keep receiving the Error: Target must be an event emitter
http://js.arcgis.com/3.9/
Line 212

error in this function:

function runQuery() {
   var queryTask = new esri.tasks.QueryTask("http://gis.phila.gov/ArcGIS/rest/services/PhilaGov/Police_Incidents_Last30/MapServer/0");
   
   var query = new esri.tasks.Query();
   query.returnGeometry = true;
   query.where = '1=1';
   query.outFields = ["DC_DIST", "POINT_X", "POINT_Y"];
   
   map.on(queryTask, "onComplete", function(featureSet) {

    var cL = new clusterlayerNew({
     displayOnPan: false,
     map: map,
     features: featureSet.features,
     infoWindow: {
      template: new esri.InfoTemplate("${SECTOR}"),
      width: 325,
      height: 100
     },
     flareLimit: 15,
     flareDistanceFromCenter: 20
       });

             map.addLayer(cL);

   });
  };
0 Kudos
11 Replies
ReneRubalcava
Frequent Contributor
Ok, I got this working, somewhat, but I'll get to that.

This works.
require([
    "dojo/Evented",
    "dojo/ready",
    "dojo/parser",
    "dojo/on",
    "extras/clusterlayerNew",
    "esri/map",
    "esri/layers/graphics",
    "esri/layers/FeatureLayer",
    "esri/layers/GraphicsLayer",
    "esri/tasks/QueryTask",
    "esri/tasks/query",
    "dijit/layout/BorderContainer",
    "dijit/layout/ContentPane",
    "dojo/domReady!"
  ],
  function (
    Evented,
    ready,
    parser,
    on,
    ClusterlayerNew,
    Map, // you had lower-case map, which can cause problems when creating a map variable
    graphics, // This is an empty object because it is not a module
    FeatureLayer,
    GraphicsLayer,
    QueryTask, // was called queryTask, biggest issue
    Query,
    BorderContainer,
    ContentPane
  ) {

    var map;
    // scope this out here. The above had same name (case matters).
    var queryTask = new QueryTask("http://gis.phila.gov/ArcGIS/rest/services/PhilaGov/Police_Incidents_Last30/MapServer/0");


    function init() {
      parser.parse();

      map = new Map("map", { // You had map = new map(), this could cause issues, probably in IE, everything breaks in IE.
        center: [-75.1700, 39.9500],
        zoom: 12,
        basemap: "hybrid"
      });
      runQuery(); // you need the map in the runQuery method, so initialize it first then do runQuery
    }

    function runQuery() {
      var query = new Query();
      query.returnGeometry = true;
      query.where = '1=1';
      query.outFields = ["DC_DIST", "POINT_X", "POINT_Y"];

      queryTask.on("complete", function (event) {
        console.debug('ClusterlayerNew is an empty object?', ClusterlayerNew);
        // This clusterlayer module is broken, it returns an empty object, even after I quickly edited it.
        // I did not try troubleshooting this
        var cL = new ClusterlayerNew({
          displayOnPan: false,
          map: map,
          features: event.featureSet.features,
          infoWindow: {
            template: new esri.InfoTemplate("${SECTOR}"),
            width: 325,
            height: 100
          },
          flareLimit: 15,
          flareDistanceFromCenter: 20
        });

        map.addLayer(cL);

      });
      // you were trying to execute this outside the scope of the function the query was defined in.
      queryTask.execute(query); 
    };

    // before, this was the class "QueryTask", the class is not initialized, thus not an event emitter.
    on(queryTask, "error", function (err) {
      alert(err.details);
    });

    // Use the ready() method as opposed to dojo.addOnLoad
    ready(init);
  });


See my comments for some of the issues you had.

One of your main issues seems to be with scope, so I would run through some material and you'll soon see where things broke.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope

Don't mix the old dojo.lib.ClassName() with the AMD loading of modules. In this case, it probably just added to the confusion.
Note - Mixing the two won't necessarily break your code (yet), but it should be avoided fro consistency.
That clusterlayerNew module is broken or something, I couldn't get it to work, but my changes above load your map and at least try to create that cluster layer.

You may want to check out a project I have been working on to do clusters in a fashion more similar to a FeatureLayer.
https://github.com/odoe/esri-clusterfeaturelayer

Hope that helps a bit.
0 Kudos
MattTenold
New Contributor III
Cool.  Thank you soo much Odoe.   The clusterlayerNew.js isn't soo much of a big deal.  I am going to have to use use a json feed anyways which means I will have to modify the clusterlayerNew.js class alot.
0 Kudos