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?
Solved! Go to Solution.
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);
Alex,
It worked fine for me in an in panel widget. Did you mis-align your requires in the widget you were testing?
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);
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);
Wow. Thanks Robert. I assume the delay was caused by the getPortal()?
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.
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?
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);
})));
},