It's all about the answers!

Ask a question

how to know it is in web context or not by using javascript for client?


jane zhou (1061068) | asked Jul 24 '18, 11:01 p.m.

 

Hi Someone who may concern,
 
    We are on 6.04 RTC,
    We noticed the server log reported the following error when we deploy the java script code to access current user.
org.mozilla.javascript.EcmaError: TypeError: Cannot read property "web" from undefined 

   The code is like this: 
dojo.provide("com.gm.getCurrentUserValueProvider");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes"); 
dojo.require("com.ibm.team.repository.web.client.session.Session"); 

(function() {
    var getAuthenticatedContributor = com.ibm.team.repository.web.client.session.getAuthenticatedContributor;   
    var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;

   dojo.declare("com.gm.getCurrentUserValueProvider", null, {

        getValue: function(attribute, workItem, configuration) {
var loggedinUser = getAuthenticatedContributor().itemId;
console.log(loggedinUser);
      }

  We noticed it can only work on web context, so, what should we do if we want to add a condition in the code, say, if it is web context, then it can run, otherwise bypass it?

    Thanks!

Best Regards,
Jane Zhou

2 answers



permanent link
Ralph Schoon (63.3k33646) | answered Jul 25 '18, 2:41 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

As far as I can tell, there is no way to detect the current user if the attribute customization does not run in the web context. This has come up here in the forum many many times and I have never seen anyone being able to come up with a solution that works in the Web as well as in the Clients.


permanent link
Lawrence Smith (3764) | answered Jul 31 '19, 5:45 p.m.
JAZZ DEVELOPER
I have found the following can be used to see if you are on the server when running a validator or value provider script.

if (typeof dojo.xhrGet === 'undefined' ) {  // is it on the server
             return workItem.getValue(attribute);
}

-- use with caution. For example, a validator may show an X on an item but if the script is to ignore the validation on the server then the save would be allowed even though the item is shown as validated.

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.