widget opened twice at first click

1398
3
Jump to solution
12-24-2017 02:36 PM
AlfonsoYañez_Morillo
New Contributor III

Hi all and Merry Christmas,

I created a very simple widget that clears the map's graphics layer. It only opens a jimu/dijits/Message which ask if you want to clear the features and two buttons, OK and CANCEL. The message dijit is created in the onOpen method and it is closed either when you click OK or CANCEL.

The weird thing is that when you start the app and click the widget button the first time, it opens two messages popups. This is because the onOpen method is called twice. The sequence is as follows:

postCreate - startup - onOpen - onClose - onOpen

However that double onOpen is not repeated for the next times you click the widget's button. I'd like to avoid that repetition.

In this widget, the only method developed is onOpen

    onOpen: function(){
      console.log('ClearGraphics::onOpen');
      var msg = new Message({
          type: "question",
          titleLabel: "Clear Map",
          message: "Want to remove features?",
          buttons:[{
              label: "YES",
             onClick: lang.hitch(this, lang.hitch(this, function(){
                    if (this.map.graphics.graphics.length > 0){
                        this.map.graphics.clear();
                    }
                    msg.close();
                    WidgetManager.getInstance().closeWidget(this.id);
             }))
          },{
               label: "CANCEL",
               onClick: lang.hitch(this, function(){
                  msg.close();
                  WidgetManager.getInstance().closeWidget(this.id);
            })
          }]
      });
    }

and the widgets properties in the manifest are:

"properties": {
    "inPanel": false,
    "hasLocale": true,
    "hasStyle": false,
    "hasConfig": true,
    "hasUIFile": false,
    "hasSettingPage": false,
    "hasSettingUIFile": false,
    "hasSettingLocale": false,
    "hasSettingStyle": false,
    "IsController": false
  }

Another question. Is it normal that the map's graphics layer starts with one graphic by default?

Happy Holidays and Thanks,

Alfonso

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Alfonso,

   In my ToggleLayerButton widget I overcome this by using a check in the code.

    var clazz = declare([BaseWidget], {
      name: 'LayerToggleButton',
      baseClass: 'widget-layertogglebutton',
      isToggling: false,
...

      onOpen: function() {
        this.setToggleLayer();
        var lObjs = [];
        array.map(this.toggleLayerIds, lang.hitch(this, function(id){
          lObjs.push(this.operLayerInfos.getLayerInfoById(id));
        }));
        if (!this.isToggling) {
          this.isToggling = true;
          array.map(lObjs, lang.hitch(this, function(lObj){
            this.toggleLayer(lObj);
          }));
          setTimeout(lang.hitch(this, function() {
            this.isToggling = false;
            WidgetManager.getInstance().closeWidget(this);
            if(lObjs[0]._visible){
              domClass.add(this.parentContainer, "jimu-state-selected");
            }else{
              domClass.remove(this.parentContainer, "jimu-state-selected");
            }
          }), 300);
        }
      }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I get the same the onOpen firing twice when first opening.

The first onOpen sets the this.isToggling to true and thus the second onOpen just bypasses the code because the this.isToggling is still true;

Some others have mentioned that the maps.graphics has a graphic by default. I have not noticed because I almost never use the map.graphics. I always create my own GraphicsLayer.

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Alfonso,

   Can I see your whole widget code?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Alfonso,

   In my ToggleLayerButton widget I overcome this by using a check in the code.

    var clazz = declare([BaseWidget], {
      name: 'LayerToggleButton',
      baseClass: 'widget-layertogglebutton',
      isToggling: false,
...

      onOpen: function() {
        this.setToggleLayer();
        var lObjs = [];
        array.map(this.toggleLayerIds, lang.hitch(this, function(id){
          lObjs.push(this.operLayerInfos.getLayerInfoById(id));
        }));
        if (!this.isToggling) {
          this.isToggling = true;
          array.map(lObjs, lang.hitch(this, function(lObj){
            this.toggleLayer(lObj);
          }));
          setTimeout(lang.hitch(this, function() {
            this.isToggling = false;
            WidgetManager.getInstance().closeWidget(this);
            if(lObjs[0]._visible){
              domClass.add(this.parentContainer, "jimu-state-selected");
            }else{
              domClass.remove(this.parentContainer, "jimu-state-selected");
            }
          }), 300);
        }
      }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I get the same the onOpen firing twice when first opening.

The first onOpen sets the this.isToggling to true and thus the second onOpen just bypasses the code because the this.isToggling is still true;

Some others have mentioned that the maps.graphics has a graphic by default. I have not noticed because I almost never use the map.graphics. I always create my own GraphicsLayer.

AlfonsoYañez_Morillo
New Contributor III

Thanks Robert, that worked.

0 Kudos