Method call after callback and event

I have a module with four functions that call one after another. I am trying to execute a popup module template. One of the functions is open, the rest are private. This happens as follows:

  • publicMethod called from another module
  • queryNames called from publicMethod
  • execute(parameters, callback?, errback?) called from queryNames
  • addNamesList invoked as an argument callback? execute
  • Multiple created dijit/form/CheckBoxand method startedquerySegments onChange
  • querySegmentsyou must call the method of the object created in publicMethod.

The problem is in step 6, I can not reach the object created in step 1.

I tried using dojo hitch to determine the argument callback?in step 3, but I can't get it to work. I tried to put thisin my first argument, but even then I cannot reach the required area for the call addNamesList.

Here are some examples to demonstrate this problem.

define([
  'dojo/dom',
  'dijit/form/CheckBox',
  'esri/layers/ArcGISDynamicMapServiceLayer',
  'esri/tasks/query',
  'esri/tasks/QueryTask',
  'dojo/_base/lang'
],
  function (
    dom,
    CheckBox,
    ArcGISDynamicMapServiceLayer,
    Query, QueryTask,
    lang
  ) {
    // ***************
    // private methods
    // ***************

    // fetch names and call addNamesList to put the list in place
    var queryNames = function (map, mapLayer) {
      // new QueryTask(url, options?)
      var queryTask = new QueryTask("url")
      var query = new Query()
      // execute(parameters, callback?, errback?)
      // this callback passes an argument called featureSet
      queryTask.execute(query, lang.hitch(map, "addNamesList", mapLayer), function(error) {console.log(error)})
    }  

    // callback function of queryNames
    var addNamesList = function (mapLayer, featureSet) {
      console.log('addOplist')
      var namesCount = featureSet.features.length
      for (var i = 0; i <namesCount; i++) {
        // work
        var cbox = new CheckBox({
          id: "cbox_" + i,
          value: featureSet.features[i].attributes["someID"],
          checked: false,
          onChange: function (evt) {
            querySegments(this.value, mapLayer)
          }
        })
        cbox.placeAt("someDiv" + i, "first")
      }
    }

    // triggered by the checkbox event
    var querySegments = function (name, mapLayer) {
        // build the query
        var queryStatement = "someID = " + name
        var layerDefinitions = [queryStatement]
        // call a method of mapLayer
        mapLayer.setLayerDefinitions(layerDefinitions)
    }

    // **************
    // public methods
    // **************
    var publicMethod = function (map) {
      var mapLayer = new ArcGISDynamicMapServiceLayer('restURL')
      map.addLayer(mapServiceLayer)
      queryNames(map, mapLayer)
      return mapLayer
    }

    return {
      publicMethod: publicMethod
    }
  }
)

You can see a more detailed explanation and a working example on this other (and wider) question that I posted in Code Review.

I'm new to JavaScript, and I think I still have a lot of problems viewing, closing, and callbacks.

I will be deeply grateful for any contribution, including how to improve this issue.

Edit

( dojo hitch) . addNamesList ( errback, , ). , , addNamesList map (hitch first argument). this , .

, :

var queryNames = function (map, mapLayer) {
  ...
  queryTask.execute(query, addNamesList)
}

var addNamesList = function (featureSet) {
  ...
    ...
      ...
        querySegments(this.value, mapLayer)
}

mapLayer , . Uncaught ReferenceError: mapLayer is not defined. .

+4
1

Javascript , , db, http- , callbacks. :

  • queryNames
  • queryNames addNamesList map
  • , addNamesList
  • mapLayer ,

, , , callback , querySegments. query, , , , :

callback(mapLayer);

, , , - callback , , , mapLayer ( , ), callback(mapLayer);.

, , .

,

+3

Source: https://habr.com/ru/post/1619571/


All Articles