Global variables in SSJS script library not working as expected

Using 8.5.3 UP1. I am having a problem with the SSJS script library which I am using for "hide / whens" or more precisely in xpages "show / ifs". In any case, the globals seem to be taking the value of the last time I saved the script library. They don't seem to be calculated based on the present value of the documents. This is a known thing (obviously not known to me). Here is an example page and library script to demonstrate the problem:

XPage example:                          

<xp:this.resources>
    <xp:script src="/ssjsVisTest.jss" clientSide="false"></xp:script>
</xp:this.resources>
<xp:inputText id="inputText1" value="#{document1.StatusTX}"></xp:inputText>
<xp:br></xp:br>
<xp:br></xp:br>
<xp:br></xp:br>
<xp:button value="Save" id="SaveBtn">
    <xp:eventHandler event="onclick" submit="true"
        refreshMode="complete">
        <xp:this.action>
            <xp:saveDocument var="document1"></xp:saveDocument>
        </xp:this.action>
    </xp:eventHandler>
</xp:button>
<xp:br></xp:br>
<xp:br></xp:br>
<xp:label id="label1" value="Status is Draft"
    rendered="#{javascript:statusVisibleDraft()}">
</xp:label>
<xp:br></xp:br>
<xp:br></xp:br>
<xp:label id="label2" value="Status is Pending"
    rendered="#{javascript:statusVisiblePending()}">
</xp:label>
<xp:br></xp:br>
<xp:br></xp:br>
</xp:view>

      

Sample SSJS script library:

var status = document1.getItemValueString('StatusTX');

function statusVisibleDraft() {

x = (status == "Draft") ? 1 : 0;

if(x > 0) {
    return true;
} else {
    return false;
}
}

function statusVisiblePending() {

x = (status == "Pending") ? 1 : 0;

if(x > 0) {
    return true;
} else {
    return false;
}
}

      

Any ideas? Thanks to

+3


source to share


3 answers


Variables in script libraries are temporary. Libraries may be unloaded between calls, depending on system load. The correct place for globals is scopes (what they are for). In your example, it seems like the viewport would be appropriate. Sven is also right about the calculations. Also, it is good practice to have no side dependencies. If you add another datasource with a different name, you won't be able to reuse the ssjs lib. You would pass the data source as a parameter, for example. in beforeRenderResult: setstatusDraftVisible (document1, 'Status', 'project') Inside such a function, you check if the field (2nd parameter) exists and has the value of the third parameter, and then set: viewScope.statusDraftVisible = TRUE; // or false

Then you selected = "# {JavaScript: viewscope.statusDraftVisible}"

When your application is more complex and you have many of these checks, you may want to consider supporting



It's faster.

+4


source


I believe this is because the status variable is not set for every call to the statusVisibleDraft () and statusVisiblePending () functions, since it is outside of the functions.

Move the assignment of the state variable inside each function:



var statusVisibleDraft = function() {

  var status = document1.getItemValueString('StatusTX');

  x = (status == "Draft") ? 1 : 0;

  if(x > 0) {
      return true;
  } else {
      return false;
  }
}

var statusVisiblePending = function() {

  var status = document1.getItemValueString('StatusTX');

  x = (status == "Pending") ? 1 : 0;

  if(x > 0) {
      return true;
  } else {
      return false;
 }
}

      

+2


source


The code in the SSJS library will only be executed if the library is loaded. The domino server caches the library internally. Only if you save the SSJS library or XPage attachment will the library be reloaded and executed again.

If you reload XPage in your browser, the SSJS library will not be reloaded or re-executed. Even if you close your browser and reopen the XPage, domino will not refresh them.

Just add some print statements to your SSJS library and you will see the behavior in the server console.

Perhaps this is the answer you are looking for.

Hope this helps Sven

Edit: This means your global variable status will only be calculated the first time you load the SSJS library.

+2


source







All Articles