Posts Tagged ‘csjs’

25 Jun 2013
2

Javascript Tip: Test to see if your variable’s value is valid

Invalid values in JavaScript come in all shapes and sizes. Sometimes you just want to know if a variable has a valid value so that you can execute the necessary code.

I had a pretty hard time with this, but I found that testing a variable for a ‘Truthy’ value works very well for me. When I say test for a ‘Truthy’ value, I mean a legit value that will return true. See below example:

1
2
3
4
5
6
7
var myVariable = 'Test';
 
if(myVariable){
   alert("True");
}else{
   alert("False);
}

The example above would return true because myVariable contains a valid value. If myVariable returned one of the following, the above example would return False:

- Undefined
- Null
- <null>
- NaN
- +0
- -0
- Empty String where length is 0.

Hope this helps :)

19 Dec 2012
5

XPages Query: Strange behavior between partialRefreshGet and Post

Hi all. I hit a very strange issue this morning with my Partial Refreshes. It’s the first time i’ve been faced with this problem. I’m happy to say that I managed to resolve the issue, but I’m a bit confused as to why it was an issue in the first place.

Ok, so I have a button that performs an SSJS Function. It also triggers a Partial Refresh of a Panel in my XPage. This Panel that gets refreshed excludes my Button Bar Panel and for very good reasons. My Button Bar Panel should only be refreshed after the initial Partial Refresh of my other Panel in the XPage.

I do this by adding some CSJS to the onComplete event of my Button that’s triggered. It uses a Remote Service to check a condition, then if that condition is True, it performs a Partial Refresh of the Button Bar Panel. I used a XSP.partialRefreshGet for the partial refresh of the Button Bar Panel.

Now, what I noticed, was that the Button Bar Panel in some cases, was been refreshed before the Primary Partial Refresh. This doesn’t happen in the first 2 times that the button is clicked, but only from the 3rd time onward. I couldn’t understand this because according to Firebug, the Partial Refreshes occur in the correct Sequence.

How I fixed the issue was quite simple: I changed the XSP.partialRefreshGet to a XSP.partialRefreshPost. This immediately resolved the issue and everything works perfectly.

In short, I am just a bit confused as to why a XSP.partialRefreshGet would run its actions out of sequence. Very strange behavior.

Would love some feedback or comments.

 

Cheers

John.

29 Mar 2012
4

Lessons learned when working with Javascript Binding Expressions in CSJS

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:

  1. All your code and logic are stored centrally in a Script Library.
  2. 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.

28 Oct 2011
4

XPages Tip: Open Links in Own window in Ext Lib App Layout

Hi Everyone. I recently found a way to have links in the Application Layout that comes with the Xpages Extension Library, to open in their own Window if you so wish. You can see a working example of this on my Xpages Showcase. Below I explain how I achieved this.

Let’s say you created a Footer Link in the Properties of the Application Layout Custom Control called “My Page“, and the href parameter is “http://www.mypage.com“, it would look something like this:

This will work perfectly, except that it will open the URL in the current window, overwriting your Xpage.

So, to open the link in its own window, you will use a simple JavaScript Function: window.open(“http://www.mydomain.com”, “_blank”);

Now, if you try to add this to the href property, it will not work, because it would be regarded as a link and not a JavaScript Function. So, to achieve the desired result, you will need to add the JavaScript Function to the onClick property. Please take note of the following 2 conditions:

1. Make sure that the href property is empty. Otherwise it will take preference over the onClick property.

2. Don’t make the onClick property computed and then add the JavaScript, otherwise it will run the Function as SSJS, which won’t work.

Simply paste the JavaScript Function as a string value in the onClick property. See below:

And that’s it. I hope this all made sense. Until next time….Happy Coding.

John