354 Lotus blogs updated hourly. Who will post next? Home | Downloads | Events | Pods | Blogs | Search | myPL | About 
 
Latest 7 Posts
Angular.js in XPages series
Wed, Apr 23rd 2014 189
Adding custom HTTP headers to Domino R9 using IBM HTTP Server (IHS)
Sun, Apr 20th 2014 266
An open letter to Mr. NotesIn9
Wed, Apr 2nd 2014 429
And the gloves are off……
Mon, Mar 31st 2014 327
When the community comes together we get the right answer
Sun, Mar 30th 2014 297
Why learning JavaScript is more critical to XPage developers than Java
Wed, Mar 26th 2014 716
PATCHing a Document using Domino Data Service
Mon, Mar 24th 2014 200
Top 10
Why learning JavaScript is more critical to XPage developers than Java
Wed, Mar 26th 2014 716
Arguably the most dangerous thing you could ever do in XPages – let other people use your eval() in their SSJS
Tue, Jun 18th 2013 430
An open letter to Mr. NotesIn9
Wed, Apr 2nd 2014 429
Why using XPages Partial Refresh is sometimes easy for developers and bad for users
Tue, Mar 4th 2014 328
And the gloves are off……
Mon, Mar 31st 2014 327
Going porn-mode on your browser
Fri, Jan 24th 2014 323
Prototypal inheritance of SSJS across the whole server in XPages
Sun, Mar 2nd 2014 317
EXTJS in XPages #14 – Grid editing and saving data via REST CRUD
Thu, Aug 1st 2013 309
When the community comes together we get the right answer
Sun, Mar 30th 2014 297
Lowering your ODS – an oldie but goodie
Fri, Feb 7th 2014 283


XPages CSJS timing issue – what is ‘this’ ?
MarkyRoden    

Problem

The JavaScript object represented by ‘this’ does not appear to be as expected.

The problem is caused because of the way that XPages adds events to DOM objects through the <xp:eventHandler>

The Situation

I wanted to pass the id from a button to a function. Outside of XPages I would normally acheive this by passing a reference to the button(this) to a function (saySomething in this case) and then extracting the id from the object passed:

function saySomething(obj){
	alert("This is my object - "+this
		+"nnThis is my id - "+
		this.id)
}

Describing 'this' when passed from a button to a function

Describing 'this' when passed from a button to a function

	<button id="markyButton" onclick="saySomething(this)"></button>

In this case I am passing the “this” object to the function.

this

is an object reference to the DOM element which it was generated from – for some serious stuff on ‘this’ check out this article

http://www.quirksmode.org/js/this.html

XPages fail :(

However when I add the JavaScript to the XPages button and add the code through the GUI Designer interface

Adding JavaScript through the XPages GUI

Adding JavaScript through the XPages GUI

I do not get the expected result when I click on the button……

Not what we expected

Not what we expected

The Explanation…

The reason this happens is because of the way that XPages assigns events to objects in the webpage. If you have ever looked at the source of an XPages you will almost certainly have seen a whole load code like this at the end of the page

XSP.addOnLoad(function() {
XSP.attachEvent(".....
}

and this is how the XPage adds the event to the object – in the case of this example the XPages creates our button in the HTML but as you can see there is no onclick event initially assigned to it

		<button class="lotusBtn" type="button" name="view:_id1:_id2:_id37:button1" id="view:_id1:_id2:_id37:button1">Click This</button>

and then at the bottom of the web page source code we can see the assignment of the event after during the “addOnLoad” which means after the page has loaded.

<script type="text/javascript">

function view__id1__id2__id37__id40_clientSide_onclick(thisEvent) {
alert("This is my object - "+this+"nnThis is my id - "+this.id)

}

XSP.addOnLoad(function() {
XSP.attachEvent("view:_id1:_id2:_id37:_id40", "view:_id1:_id2:_id37:button1", "onclick", view__id1__id2__id37__id40_clientSide_onclick, false, 2);
});

</script>

What is happening?

The XPage is adding the function “view__id1__id2__id37__id40_clientSide_onclick” to the “onclick” event of the “view:_id1:_id2:_id37:button1″ DOM element (our button in this case).

The nuances of this are subtle but important – the code is not adding “alert(whetever)” to the onclick event it is adding a function call to the onclick event.

So. when the button is clicked the function view__id1__id2__id37__id40_clientSide_onclick executes it’s code and in that situation ‘this’ no longer refers to the button.

The difference is clear when we look at the resulting code side by side

<button value="or Click this" id="button3" onclick='alert("This is my object - "+this+"nnThis is my id - "+this.id)'></button>

or

<button value="or Click this" id="button3" onclick='view__id1__id2__id37__id40_clientSide_onclick()'></button>

“this” is never passed to the view__id1__id2__id37__id40_clientSide_onclick() function and therefore any reference to the button is lost.

When the function executes – “this” refers to the function (which isn’t an object) and therefore we get the window (the webpage) as a failover.

The solution

If you need to do something like this then add the onclick event to the source pane.

Here is the XML markup for the XPages generated event (which fails me)

		<xp:button value="Click This" id="button1">
			<xp:eventHandler event="onclick" submit="false">
				<xp:this.script>
					<![CDATA[alert("This is my object - "+this+"nnThis is my id - "+this.id)]]>
				</xp:this.script>
			</xp:eventHandler>
		</xp:button>

and the XML markup  for the manually added onclick event (which works)

		<xp:button value="or Click this" id="button2"
			onclick='alert("This is my object - "+this+"nnThis is my id - "+this.id)'>
		</xp:button>

The demonstration

Here is a simple demo of the problem and the solution




---------------------
http://xomino.com/2012/04/09/xpages-csjs-timing-issue-what-is-this/
Apr 08, 2012
52 hits



Recent Blog Posts
189


Angular.js in XPages series
Wed, Apr 23rd 2014 7:37p   Mark Roden
Introduction Angular.js is an Open Source (MIT) JavaScript framework created and maintained by Google. It is designed to help web developers create quick and functional applications, while reducing the overhead of data binding and state management. If you want to find out more about Angular then look at some of the myriad of great resources around the web. Angular Developer Tutorial New To Angular? Start Learning Here https://www.ng-book.com/ Angular.js the awesome parts (Video) Front end / [read] Keywords: domino xpages application applications community development google java javascript
266


Adding custom HTTP headers to Domino R9 using IBM HTTP Server (IHS)
Sun, Apr 20th 2014 6:41p   Mark Roden
IBM HTTP Server (IHS) was added to R9 so that we could have TLS support for HTTPS encryption. For more on how to do that see Russ Maher’s post(s) on the subject. Under the covers IHS is really a modified version of Apache Web server and comes with some of the Apache options. It gives us the ability to add custom headers to all pages served up by the server. You can turn IHS on by adding the following to your notes.ini – this does require a full reboot HTTPIHSEnabled=1 When the [read] Keywords: domino ibm notes xpages server
429


An open letter to Mr. NotesIn9
Wed, Apr 2nd 2014 6:33a   Mark Roden
Dear Mr Leedy, I regret to inform you that your blatant use of child labor has offended my child labor union. They do not feel like you have represented me in the correct light. I trust that you will find this short response in the same light as your original offense. yours faithfully, Your very good friend Marky Roden CC: everyone who has seen this video http://notesin9.com/index.php/2014/03/31/notesin9-141-java-vs-javascript-throwdown/ [read] Keywords: java javascript
327


And the gloves are off……
Mon, Mar 31st 2014 5:57p   Mark Roden
Picture this: I have been away for 5 days and during that time “someone” has been posting cryptic comments on twitter about how the next notesin9 is going to be different….and scaring me. So I am waiting at the airport this afternoon, about to board my plane home and I get this link from this “friend” who tells me to watch the new video it and enjoy. I download it and watch it on the plane. People start to look at me funny as I start to snort and giggle at my phone. [read] Keywords: ibm xpages application integration javascript twitter
297


When the community comes together we get the right answer
Sun, Mar 30th 2014 12:41p   Mark Roden
Last week I posted why JavaScript is more critical a skill to learn than Java. There was an outpouring of comments, discussion and responses unlike any I have seen in a while. To give a little more background as to why I decided to post: I was not to be antagonistic or instigate a storm in a tea cup. It was because for the third time in 2014 I had been told by a notes developer something along the lines of “I can’t learn XPages because I have read that you need to learn Java and I do [read] Keywords: notes xpages blogging community java javascript
716


Why learning JavaScript is more critical to XPage developers than Java
Wed, Mar 26th 2014 5:14p   Mark Roden
I have tried to write this article multiple times over the last 2 years, when I read it back to myself it always sounds like I am bitching. Honestly, I usually am and that is why it has not been published. This is a final attempt at a constructive argument against the insistence on many blogs that everyone should learn Java. Many people think I hate Java; I really don’t. I can program Java (I am no expert admittedly) and have done so on multiple occasions. My concerns are always raised whe [read] Keywords: domino ibm lotus notes notes client R4 xpages ajax application applications community css database development dojo google interface java javascript network server websphere xml




200


PATCHing a Document using Domino Data Service
Mon, Mar 24th 2014 7:11p   Mark Roden
Talk about frustrating – in a week full of slow progress and CORS cross domain hell I found this little annoyance after hours of staring and curing – once again the power of trial and error triumphs over my stupidity again. The problem I have a Domino Data Service running on the server and I want to update a document This is the imaginary URL https://xomino.com/issues.nsf/api/data/documents/unid/FDCA9C28A793D3F785257C4D0068BBCA returning the imaginary data just fine { "@href": [read] Keywords: domino ibm ajax application java server
224


How to ad icons to individual items in a Select2 multi-value field
Tue, Mar 18th 2014 8:08p   Mark Roden
Select2 is one of the best user interface enhancers I have come across – if you do not know what it is then you need to go play with it. It transforms SELECT boxes into dynamic, stunning, interactive UI elements and allows for all sorts of customizations and developer fun. What I want to be able to do is select items from different categories within the select2 box and then add an icon displaying to the user which category they came from. In this article I will show how. Example I want to [read] Keywords: css interface xml
187


My ongoing struggle with stupidity and what is not valid JSON
Sun, Mar 16th 2014 6:06p   Mark Roden
So this was a frustrating lesson to learn – and please feel free to be entertained by my apparent stupidity. I was trying to create my own JSON with an xAgent – seems easy enough. I created a simple output which looked like this { 'items': [ { 'name': 'AK', 'dc': 23 } ] } and when I fed it through my AJAX code it failed. $.ajax({ dataType: 'json', url: "http://copper.xomino.com/xomino/ExtjsBigData.nsf/xState.xsp?query="+query //url: 'http://coppe [read] Keywords: ajax google javascript
213


jQuery in XPages #20 – NProgress – YouTube-like slim progress bar
Wed, Mar 12th 2014 7:02p   Mark Roden
In this article I will describe how to implement and use the jQuery NProgress nano scrollbar plugin Demonstration The XPages integration of NProgress is demonstrated here Download The demonstration database can be downloaded from the link above or from here NProgress Introduction Since Youtube.com added their nano scroll bar at the top of the page there have been a flurry of different jQuery plugins which mimic the nano progress bar at the top of the screen. The silly thing is that the progre [read] Keywords: xpages ajax application applications css database dojo integration javascript openntf server xml




Created and Maintained by Yancy Lent - About - Blog Submission - Suggestions - Change Log - Blog Widget - Advertising - Mobile Edition