Search
Categories

Entries in Code Snippets (3)

Sunday
30Aug2009

Prevent Partial Update via Client Side Javascript - Did You Know?

You can cancel the partial submission (and subsequent server-side event execution) by having the associated client side event "return false".

For example, if you have a server action that deletes a document, you can attach a client side script to confirm the action in the same event, before the submission occurs, and cancel the partial submission. In the Event property page, select the "client" tab for the same event you have the server-side code, and enter code similar to:

if (!confirm("Are you sure you want to delete this document?")) { return false; };

/* More code could go here, and would get executed if the user clicks "Ok" */

This will prevent any code after the confirm from running if the user clicks "Cancel", including the submission of the partial update.

For example, you could fade out the document using dojo.fadeOut() before sending the partial update to the server, adding a bit of flair to your application (and everybody wants to have more than just the minimum required pieces of flair, right?).

Illus. 1 - Server Side Script that runs "onClick"

Illus. 2 - Client Side Script - First Line does a confirm, like the code above

Happy Coding!

Wednesday
12Aug2009

Adding content to the HTTP Header of an XPage

Sometimes it is necessary to add some content to the HTTP header of a servers response. Some of you may have already dealt with such before and therefore have wondered how this is accomplished with XPages. For those of you, who doesn't here's a small example why one would ever like to do that:

Suppose you want to tell the client the contents language you are about to send - this is done by the Content-Language header field. A more common approach is specifying how the browsers cache should handle this particular content (that is being allowed to cache on disk or to reload the URL every single time you access it), then you would want to use the Cache-Control header field. Those of you who have dealt with the specialties of Internet Explorer 8 may want to make IE8 behave like IE7 for some reason - which is done by the X-UA-Compatible header field and can make IE8 behave pretty much like an Internet Explorer 7. If you look at your Internet Explorers address bar you have certainly seen the sometimes appearing compatibility icon that can make a Website look better in IE8. The X-UA-Compatible header is about enabling that button rather than requiring the user to click it.

But how can one add such a header field with XPages? The answer is: very easy - you need only one line of code (but you will use more for better readability/maintainability of your code).
Let's have an example. We will take the IE7 compatibility field but the example can be easily customized to be used for other fields like cache directives or the like.

At first you will need to open the Events section of your XPage (or custom control if you want to reuse the code), navigate to the beforeRenderResponse event and switch to the Script Editor code type:



Then you need to add only the following line:
facesContext.getExternalContext().getResponse().setHeader("X-UA-Compatible", "IE=EmulateIE7");
This one single line will add the X-UA-Compatible header field and will place the content IE=EmulateIE7 to it. For the purpose of making IE8 behave like an IE7 this would be already enough, but since we want to have maintainable code, we will make the code probably a bit longer:

try {
  if (context.getUserAgent().isIE(8, 8)) {
    var response = facesContext.getExternalContext().getResponse();
    response.setHeader("X-UA-Compatible", "IE=EmulateIE7");
  }
} catch (e) {
  print("Something bad happened in the IE8 detection: " + e);
}

These few lines will not only handle any possibly occurring error and throw that to the servers console but will also place the header field only to responses generated to an Internet Explorer 8 browser - other browsers would ignore it anyway.

Saturday
18Jul2009

Great XPage Wiki

Of course the golden standard of Wikis regarding Notes and Domino Development is the Domino Designer Wiki by IBM. On checking this today I see 92 entries related to XPages.  A lot of good stuff in there.

However, as good as that is, there is a Wiki that I like better and spend more time digging through.  

YouAtNotes XPages Wiki

The Home page says that this isn't meant to be a competitor to the Official Wiki, it's an extension.  This is a collection of code snippets and tips that Julian Buss keeps for his personal use and makes available to the community.  According to the home page, other people are allowed to contribute and comment as long as they play nice.

What I love about this wiki is that these are bite size snippets related to XPages.  You don't have to reverse engineer a full application just to try to get to the piece your looking for.

This site is what really got me feeling comfortable with using Server Side JavaScript to access the backend objects and start actually doing things. I think it's a great resource and suggest anyone getting into XPages development should check it out.