AnsweredAssumed Answered

Returning a value from dojo.connect

Question asked by filipkral on Jul 31, 2014
Latest reply on Aug 13, 2014 by filipkral


I have quite a few calls like this in my JavaScript:

dojo.connect(map, "onLoad", {map: map, do:buildScalebar}, "do");


and I would like to simplify to something like this:

var scalebar = buildScalebar({ map: map });
// the retuned value should be the scale bar widget so I could do:
scalebar.hide();; // and so on


What is the best way to do it?

Here is the buildScalebar function from the second case, which however returns null.

    function buildScalebar(opts){
      /* Build the scalebar widget with options:
      * .map -- map for which to load the scalebar
      * .unit -- scalebarUnit, dual|metric|english, default is "dual"
      * .anchor -- top-right|bottom-right|top-center|bottom-center|bottom-left|top-left, default value is "bottom-left"
      * Returns the widget. (BUT IT DOESN'T!!!)
      var o = this;
      if ('map' in opts) { o = opts; }

      var map =;
      var anchor = 'anchor' in opts ? opts.anchor : "bottom-left";
      var unit = 'unit' in opts ? opts.unit : "dual";

      var scalebar = null;
      dojo.connect(map, "onLoad", {
        "map": map,
        "attachTo": anchor,
        "scalebarUnit": unit,
        "do": function () {
          var options = {"map":, "attachTo": this.attachTo, "scalebarUnit": this.scalebarUnit }
          scalebar = new esri.dijit.Scalebar( options );
      }, "do");
      return scalebar;


I would like to use this pattern for other widgets and functionality, not just the scale bar widget.

Is it a good idea? Do I need some kind of Deferreds here? Can you help me with an example?