using portalUtils to get current logged user won't work for off-panel widgets

4091
7
Jump to solution
11-10-2016 11:11 AM
Alexwang
Occasional Contributor II

Hello, I was trying to use portalUtils to get the current logged user in an off-panel widget like HomeButton, but returned null. I didn't have this issue with any in-panel widgets. So using portalUtils to get current logged user doesn't work for off-panel widgets? How to get the current logged portal user in off-panel widgets?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Alex,

  OK that is just a timing issue then.

Place this in the startup in the homebutton widget:

        setTimeout(lang.hitch(this, function(){
          this.portal = portalUtils.getPortal(this.appConfig.portalUrl);
          this.username = this.portal.user.username;
          console.info(this.username);
        }), 1000);

View solution in original post

7 Replies
RobertScheitlin__GISP
MVP Emeritus

Alex,

   It worked fine for me in an in panel widget. Did you mis-align your requires in the widget you were testing?

0 Kudos
Alexwang
Occasional Contributor II

Thanks, Robert. Yes, it works for in-panel widgets, but not for off-panel widgets. for instance, if i put the codes in the HomeButton startup(), it returns null. 

this._portal = portalUtils.getPortal(this.appConfig.portalUrl);
console.log(this._portal.user);

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Alex,

  OK that is just a timing issue then.

Place this in the startup in the homebutton widget:

        setTimeout(lang.hitch(this, function(){
          this.portal = portalUtils.getPortal(this.appConfig.portalUrl);
          this.username = this.portal.user.username;
          console.info(this.username);
        }), 1000);
Alexwang
Occasional Contributor II

Wow. Thanks Robert. I assume the delay was caused by the getPortal()?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

No, it has to do with the off panel widgets life cycle and then getting called much quicker then in panel widgets and so much other app initializing happening at the same.

0 Kudos
Alexwang
Occasional Contributor II

Robert, setTimeout() worked, but instead of using a hard way of delay, do you know if there are other ways to handle this such as a method with a deferred return object, or an event to fire when the user object loading was completed?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Alex,

  OK, it looks like I found a good event to listen for (line 26 - 30):

      startup: function() {
        var initalExtent = null;
        this.inherited(arguments);
        this.own(on(this.map, 'extent-change', lang.hitch(this, 'onExtentChange')));

        var configExtent = this.appConfig && this.appConfig.map &&
          this.appConfig.map.mapOptions && this.appConfig.map.mapOptions.extent;

        if (configExtent) {
          initalExtent = new Extent(
            configExtent.xmin,
            configExtent.ymin,
            configExtent.xmax,
            configExtent.ymax,
            new SpatialReference(configExtent.spatialReference)
          );
        } else {
          initalExtent = this.map._initialExtent || this.map.extent;
        }

        this.createHomeDijit({
          map: this.map,
          extent: initalExtent
        });

        this.own(topic.subscribe("preloadWidgetsLoaded", lang.hitch(this, function(){
          this.portal = portalUtils.getPortal(this.appConfig.portalUrl);
          this.username = this.portal.user.username;
          console.info(this.username);
        })));
      },