How can I establish a scope variable through client-side JavaScript in XPages?

My goal is to update an XPages scope variable using Client-side JavaScript. I have a complex XPage with multiple sections that are toggled on and off using Dojo. Within this XPage, there is a button that triggers some Server-side JavaScript. However, after the SSJS runs, the default visible section reappears instead of the section that was visible before clicking the button. I want to maintain the visibility of the section that was active before the button click even after executing the SSJS.

My idea is to utilize a scope variable - employ Client-side JavaScript to determine the current visible section on the XPage, store this in a scope variable, and then retrieve and display this value during the onClientLoad event of the XPage to ensure the correct section is visible (while hiding all others). However, I have not been able to find a way to set a scope variable directly from Client-side JavaScript. I attempted to add

var xyz = "#{javascript:viewScope.put('sectionDisplay','Section')}"

to the onClick event of the button, but this erroneously sets the scope variable even when the button is not clicked.

Prior to XPages, passing variables using query strings between pages was common. How can I accomplish this functionality now?

Answer №1

Setting the CSJS variable in this manner is not recommended: When you include this code in your CSJS, it will be evaluated prior to being sent to the browser. This results in the variable being set regardless of whether the button is clicked or not.

Instead, you can incorporate some SSJS code within your button to define the viewScope variable. You can transmit the value from the browser using a parameter, as outlined in this resource:

UPDATE:

In SSJS, you can access various parameters using the param object. For example, if you include the parameter value in your CSJS...

XSP.partialRefreshGet(id, {
    params: { 'value': 'some string'}
});

... you can retrieve the parameter in SSJS like this:

param.get("value");

Answer №2

The best approach depends on the specific scenario you are dealing with. If you prefer to utilize partial or full refresh, you can incorporate a Hidden edit element within XP, linking it to anyScope.variable_name, and updating its value using CSJS:

  var element = document.getElementById("#{id:hiddenInput1}");
  element.value = "updated content";

Upon each refresh, the modified value will be sent to the model and stored in the scoped variable for future reference.

Alternatively, you can implement an event handler using SSJS to manipulate the scoped variable according to the value of the hidden input or the value submitted, as suggested in Sven's response.

Answer №3

Consider an alternative strategy: instead of relying on the scope variable, you may choose to hide an element by adjusting its style property to "display:none".

Imagine you have a div like this:

<xp:div id="mydiv">...</xp:div>

Here's an example of how you can achieve this using CSJS in a button:

document.getElementById("#{id:mydiv}").style.display="none"

Answer №4

XSP.partialRefreshGet( id, {
    params: { 'value': 'some string'}
});

... you can retrieve the parameter in SSJS using:

param.get("value");

I am transferring a unique identifier from a grid to a dialog box. While I am able to fetch param.get('unid'), I am facing difficulty setting it to the document data source unique identifier. This prevents the fields from being populated with values from the backend document. Since there are numerous fields, I am exploring options to populate the data source without manually entering values in each field. Is there a way to make the document data source recognize the param.unid? I attempted using a hidden input as suggested, but the value is not being submitted. Is this a viable approach?

XSP.partialRefreshGet(dialogId,
{
    params: xspParams,
    onStart: function(){
        //dojo.style(loadingGifId,'display','block');
        XSP.getElementById("#{id:inputText1}").innerHTML = unid; //not functional
        XSP.getElementById("#{id:inputText1}").value = unid; //also not functional
    },
    onComplete: function(){
        dijit.byId(dialogId).show();
        //dojo.style(loadingGifId,'display','none');    
}});


<xp:panel id="dialog">
    <input id="inputText1"
        value="#{javascript:sessionScope.personUNID;}">
    </input>

    <xp:this.data>
        <xp:dominoDocument var="docPerson" action="editDocument"
            formName="fUserName">
            <xp:this.documentId><![CDATA[#{javascript:sessionScope.personUNID ;}]]></xp:this.documentId>
        </xp:dominoDocument>
    </xp:this.data>

    <xp:inputText id="cfParams"
        value="#{javascript:param.get('unid');}">
    </xp:inputText>
</xp:panel>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve data from MongoDB using the find() method results in an empty response, however,

While working on a project to practice my MongoDB skills, I encountered an issue with retrieving all the data from MongoDB. Despite receiving a successful 200 response, I was unable to properly extract all the data. Using Express framework for this task, ...

What steps can I take to stop the browser from refreshing a POST route in Express?

Currently, I am using node along with stripe integration for managing payments. My application includes a /charge route that collects various parameters from the front end and generates a receipt. I am faced with a challenge on how to redirect from a POST ...

Encountering a ng-select2 Error with Angular version 4.1.3

I have recently installed the ng-select2 package, but I encountered an error when trying to compile my code using 'ng serve'. Node version: 8.10.0 NPM version: 6.0.0 Another list item Operating System: Windows 7 ERROR in d:/PATH-TO-PROJECT-F ...

Issue: My application is unable to start due to the module nuxt.js not being found. Can someone help me troubleshoot

Upon attempting to execute npm run dev following the installation of dependencies, I encountered an error that has left me puzzled. Despite trying various solutions found online, none have seemed to resolve the issue. <a href="/cdn-cgi/l/email-protectio ...

Why is there a node_modules folder present in the npm package I authored when using it as a dependency in another module?

After successfully launching my first npm package, I noticed something strange when installing it as a dependency in my project. Upon exploring the project folder in node_modules, I discovered an unexpected node_modules folder containing just one package ...

Using AngularJS to differentiate between desktop and mobile devices, as well as determine if the connection is wifi or not

After some research on SO, I couldn't find similar Q&A about detecting connection types rather than just if a connection exists or not. The goal of my website is to automatically play a video clip based on the user's device and network statu ...

Toggle visibility of div based on current business hours, incorporating UTC time

UPDATE I have included a working JSFiddle link, although it appears to not be functioning correctly. https://jsfiddle.net/bill9000/yadk6sja/2/ original question: The code I currently have is designed to show/hide a div based on business hours. Is there a ...

Utilizing a distinct template with ui-router: A step-by-step guide

I am currently working on an Angular application that utilizes ui-router for view routing. Within my master template, I have all the layout elements set up, including my ui-view as shown below: <div class="content" ui-view> <div> Here are my ...

What is the proper way to retrieve the app object from a module in ExpressJS?

In my application, I am running an Express app by setting it up in the app.js file: var app = express(); Once the app is created, I can define variables like this: app.set('host', 'myhost') Now, within my project, I also have a sepa ...

The ChromeDriver capabilities that have been configured are not maintained once the WebDriver is constructed in Node Selenium

I am currently experimenting with adding the default download path using Chrome capabilities in my code snippet below: const test = async () => { let builder = await new Builder().forBrowser("chrome"); let chromeCapabilities = builder.getC ...

Creating a basic Angular 1 application from scratch

Currently, I am in the process of setting up a small application with Angular 1 to enhance my skills (yes, we are still using version 1 at my workplace and I want to familiarize myself with it). Most of the application is working fine, but as soon as I in ...

What is the best way to create height segments from a cylinder in three.js?

My current project involves a cylinder that is divided into multiple height segments, with the number of segments depending on the specific data. Each height segment contains a value that I want to use for extruding the entire circle at that particular hei ...

Is it possible for JavaScript to interact with elements that are created dynamically?

It's puzzling why the newly generated elements remain unseen by the next function that is called. Any insights on how to address this issue would be greatly appreciated! Resolve: Incorporate async: false to deactivate the asynchronous feature in order ...

Transmit intricate data structure to a web API endpoint using AJAX requests

When attempting to send a complex object named vm containing an object 'Budget' and an array 'BudgetDetails' populated with rows from an HTML table to my API controller using AJAX, I encounter the error message "Uncaught RangeError: Max ...

Having trouble installing gatsby-plugin-transition-link using npm

https://i.stack.imgur.com/DyZxQ.png I'm facing some issues while trying to install gatsby-plugin-transition-link using npm. No matter what solutions I've attempted, the errors persist. Can anyone provide insight into what might be causing this p ...

Can you explain the distinction between using single and double quotes in my JavaScript and CSS code?

Is there a significant distinction between using single or double quotes in JS and CSS? Does it depend on personal preference, or are there certain instances where one is preferred over the other? In W3Schools, they employ single quotes for url('&apo ...

What could be the reason my black overlay doesn't show up when the button is clicked?

Attempting to craft a pop-up window on my own, I encountered an issue. Upon pressing the button, instead of the anticipated pop-up appearing and darkening the background below it, the entire page freezes with no sign of the pop-up box. If I eliminate the ...

What are the steps to implement THREE.MeshLine in my JavaScript project?

While exploring ways to create a line using three.js with a modified width, I stumbled upon this article suggesting the usage of a THREE.MeshLine class available here. However, when I attempted to follow the documentation to implement it in my script, I en ...

What could be causing the JavaScript to malfunction when combined with PHP?

Attempting to retrieve search items from Twitter using PHP curl, process, and update them every second. For this basic version, the goal is to refresh the search results for "bieber" every second. The issue seems to arise from combining PHP with JavaScript ...

What is Angular's approach to handling elements that have more than one directive?

When an element in Angular has multiple directives, each specifying a different scope definition such as scope:false, scope:true, or scope:{}, how does the framework handle this complexity? ...