Hi everyone. Here’s a quick note on something i struggled with yesterday, but managed to resolve with the help of Tommy Valand and Paul Withers. Thank you gents
.
I find myself working more and more with Client Side Javascript to make use of Dojo, but still need access to Scoped Variables and Server-Side Objects. A typical way of running or accessing Server-Side objects is by using Javascript Binding Expressions in your CSJS Editor. Below are 2 examples of Javascript Binding Expressions:
- “#{javascript:getComponent(‘ControlID’).getValue();}”
- “#{javascript:viewScope.VariableName}”
PROBLEM 1
As a best practice I store all my functions in Script Libraries, and not directly in XPages. The advantages of this are:
- All your code and logic are stored centrally in a Script Library.
- If you need to modify any of your Script Libraries, you don’t need to re-build your Notes Database
So the problem I faced here is that my Javascript Expressions were not evaluating. They were returning null values even though I know for a fact that my expressions are correctly set.
Understanding Why
The reason my expressions weren’t evaluating is because they can only work in a XPage, Custom Control or Theme Design Element, and not a Script Library or external JS File.
Workaround
The solution to this is to run these Expressions inside the XPage or Custom Control where you have your function, and then pass the result of your expressions to the Javascript Function that exists in your Script Library.
I myself am not too crazy about this because now there’s more code that I have to maintain inside my XPages vs Script Library, but it’s not the end of the world.
PROBLEM 2
So now I placed my Javascript Binding Expressions in one of my XPages, within the onComplete Event of a button. They were no longer returning null values, but instead returning Empty String Values.
A Quick Scenario: A User creates a new document, and when clicking on the button, I have SSJS code that populates fields and Scoped Variables, and performs a partial refresh. The idea is that my code in the OnComplete event grabs the new values populated and uses Dojo to manipulate the page.
Understanding Why
The reason why my expressions were now returning Empty String Values is because the functions within the Javascript Expressions are rendered on Load of the XPage.
i.e. “#{javascript:viewScope.VariableName}” was rendered when the page was created, not when the button was clicked. The Scoped Variable was empty at that stage of the Page Loading.
Workaround
There is a simple way around this, and it’s called a Remote Service. This is a Custom Control that ships with the XPages Extension Library. I’ve created a Video Tutorial that explains how this Service Works. In Short, a Remote Service allows you to call SSJS Functions within Client-Side Javascript, as well as passing parameters and getting a handle result.
Click Here to view the Tutorial on how to use Remote Services in your XPages.
I trust you found this Blog Post useful. I always appreciate feedback or better alternatives if you have any.
Until next time.
Cheers,
John.
Hello!
Thank you for the example! I’m trying to use it, but for some reason it doesn’t work at all. I have 8.5.3 UP1, and I can see the Remote Services custom control.
Basically, what I want to do is set a viewScope variable, and then refresh multiple panels. These panels can’t be stuck in one place, as it’s a complex form.
So my idea is to use Remote Services to get a handle on the radio button, set the viewScope variable, then continue in CSJS by refreshing multiple panels. Simple enough, eh?
Thanks & keep up the good posts!
Steve in NYC
Hi Steve. Than you for your reply. If I understand your comment correctly, you have a button that when clicked, will get a handle on a Radio Control, populate a viewScope Variable and then perform a couple of Partial Refreshes?
If I’m right, I wouldn’t even use a Remote Service, except If I wanted to pass SSJS values to my CSJS Code. I would write SSJS to get a Handle on the Radio Button and populate the View Scope. On the OnComplete event of the button, I will then run the multi Partial Refreshes using XSP.partialRefreshPost.
Here’s an article on how to achieve this. Let me know if I didn’t understand correctly.
Cheers.
http://xpageswiki.com/web/youatnotes/wiki-xpages.nsf/dx/Work_with_events_and_partial_or_full_refresh
Hello & Happy New Year!
Thank you for responding. I only just got back to work after the Holidays, so I’m catching up on some of the things I need to finish this project.
I think I’m close… I’m not using a button, but rather the onChange event of the radio button group to change the viewScope and the partial refresh for several panels.
I think what is confusing about Tim’s article is that he says the the XSP.partialRefresh code is SSJS, however, it only works for me in CSJS.
So, when I set the viewScope on SSJS, and do a full update, everything works fine. If I add the partial refreshes to SSJS, I get an error “XSP is not found”. When I add the partial refreshes in the CSJS of the radio button, it doesn’t work because the viewScope is set in the SSJS, or after the CSJS.
I’m sure I’m just missing something silly, but any pointers to help me solve this riddle would be greatly appreciated.
Thanks, in advance!
Steve in NYC
Hi Steve. A very happy new year to you as well
You are faced with 2 issues regarding your above example:
1. You are correct that XSP.partialRefreshPost is CSJS and not SSJS.
2. The other problem is that a RadioGroup Control does not always trigger properly on the browser. So your onChange code will not always fire when a users clicks one of the Radio Options. I don’t know why this is, but it has something to do with Radio Controls on the Browser itself, and not something that’s related to XPages.
Could you perhaps send me the XSP Code from your page of what you are trying to do with the Radio Group Control.
Once I see it I’ll be able to assist.
You can send it to john.jardin@gmail.com.
Many thanks.