mx session loaded

4
Hi, re: Different index pages per user role question. I have investigated why the themes per user role were being set in an intermittent manner and I just found out that when I get the mx.session, some times the call mx.session.isLoaded() returns false. I have tried running my code which basically gets the user role after login mx.session.getUserRoles("Name") without the mx.session.isLoaded() check, and at that same occasion when isLoaded() returns false, the getUserRoles is also empty. this is an example extract: if (mx.session&&mx.session.isLoaded()) { var mendixRoles = mx.session.getUserRoles("Name"); } Is there a particular reason why the session is not loaded? Any help here is much apreciated! Regards, Luis.
asked
3 answers
9

As Jasper mentioned, your code is being called before the client environment has finished loading.

Instead of manually checking if parts of the system are loaded, it is probably easier to wrap your code in a function and pass that to mx.addOnLoad(), which will invoke that function after the whole environment has finished loading.

mx.addOnLoad(function() {
    console.info("Client environment has finished loading");
    var roleNames = mx.session.getUserRoles("Name");
    for (var i=0; i<roleNames.length; i++) {
        console.info("User has role " + roleNames[i]);
    }
});
answered
4

I am not a programmer for the client so I'm not sure if my answer is completely correct. So please correct me if I'm wrong.

The client can do almost everything asynchronously. That is probably the cause of your problem. When you login to your application, the client sends a request to the XAS to retrieve the user, the role for the user and some other useful information.
While the client is waiting for the response, the browser continues with loading the page with your code as well.
If it takes a 'long' time before the client receives the info. Than the browser has loaded your startpage and has executed the javascript code before the client knows which user is logged in. In that case the function session.isLoaded() will return false.

Why do you want this and what kind of actions are you trying to accomplish with your javascript? If you can give some more information maybe we can help you suggest the best solution for the problem.

answered
0

Hi Jasper,

Thank you and Michiel for your answers. I will certainly have a go with the mx.addOnLoad() function. My functions already trigger on the normal onLoad event, but maybe I need to somehow delay the process a bit further. The object is really just to change some paths in order to use different the custom.css from different themes, and thus allow the customer to even costumise his/hers look and feel if they want to. Once the getUserRoles returns a value, it works absolutely fine, but with a false isLoaded I'm not able to change those themes.

I will have a go at trying the addOnLoad function as it seems to me the problem is really the timing of execution. Thank for your help.

Luis.

answered