<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Veggerby : IBlog &#187; jQuery</title>
	<atom:link href="http://blog.veggerby.dk/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.veggerby.dk</link>
	<description>Low decoupling, highly incoherent</description>
	<lastBuildDate>Fri, 28 May 2010 19:47:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>jQuery and AJAX Tooltips</title>
		<link>http://blog.veggerby.dk/2009/01/01/jquery-and-ajax-tooltips/</link>
		<comments>http://blog.veggerby.dk/2009/01/01/jquery-and-ajax-tooltips/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 19:38:58 +0000</pubDate>
		<dc:creator>nosey</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript jquery tooltip]]></category>

		<guid isPermaLink="false">http://blog.veggerby.dk/2009/01/01/jquery-and-ajax-tooltips/</guid>
		<description><![CDATA[So the day has dawned, very symbolic that this day happens to be the first of a new year… enough of that. The day has finally come where I post a JavaScript entry! Who would have thought that??? Over the last couple of months I have had a very passionate relationship with a new hottie [...]]]></description>
			<content:encoded><![CDATA[<p>So the day has dawned, very symbolic that this day happens to be the first of a new year… enough of that. The day has finally come where I post a JavaScript entry! Who would have thought that???</p>
<p>Over the last couple of months I have had a very passionate relationship with a new hottie I discovered. She is named <a href="http://jquery.com/">jQuery</a> and OMG can she do tricks… OK, stop this talk right now <img src='http://blog.veggerby.dk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Basically I have had my head in <a href="http://akax.asp.net/">AJAX for ASP.NET</a> doing what <a href="http://mattberseth.com/blog/2008/06/getting_jquery_goodness_into_a.html">others</a> <a href="http://mattberseth.com/blog/2008/06/getting_jquery_goodness_into_a_1.html">before</a> <a href="http://www.west-wind.com/WebLog/posts/400348.aspx">already</a> realized probably wasn’t the best approach. Finally I got around to have a look at jQuery and I must admit that (especially for my <a href="http://asp.net/mvc">ASP.NET MVC</a> project(s)) I haven’t looked back!!!</p>
<p>Today I had a little feature I wanted to solve with some AJAX:</p>
<p>I have a list of objects displayed in a certain view. One property on these objects is calculated and involved some semi-heavy database and calculation stuff and therefore requires both time and system resources to get. This value is not in particular important to the current view, but is a nice-to-have information. This means that the value as such does not need to appear in the view after rendering. I therefore figured it could be nice to have in a dynamic tooltip, i.e. “on tooltip hover, calculate/retrieve the value and display it as a tooltip”.</p>
<p>Having used other of <a href="http://bassistance.de/jquery-plugins/">Jörn Zafferer</a> jQuery plug-in’s previously (with great success) I turned my attention to his <a href="http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/">tooltip plugin</a>. </p>
<p>Having read the <a href="http://docs.jquery.com/Plugins/Tooltip">documentation</a> it is apparent that, while you can specify a callback function (namely the “bodyHandler” property) to determine which “value” to display in the tooltip, there is an issue with this: it is synchronous! And by <a href="http://en.wikipedia.org/wiki/AJAX">definition</a> AJAX is asynchronous. The problem here is that if you do a <a href="http://docs.jquery.com/Ajax/jQuery.get#urldatacallbacktype">$.get()</a> call you the return value of the bodyHandler callback is “out of scope”, since it is first available once the AJAX call is completed, i.e.:</p>
<div class="vscode">$(<span style="color: #a31515">&quot;.tooltip-element&quot;</span>).tooltip({     <br />&#160;&#160;&#160; bodyHandler: <span style="color: #0000ff">function</span>() {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $.<span style="color: #0000ff">get</span>(<span style="color: #a31515">&quot;/MyController/MyAction&quot;</span>, { id : <span style="color: #a31515">&quot;myid&quot;</span> }, <span style="color: #0000ff">function</span>(data) { <span style="color: #008000">/* do what with &quot;data&quot;? */</span>; }, <span style="color: #a31515">&quot;html&quot;</span>);     <br />&#160;&#160;&#160; },     <br />&#160;&#160;&#160; showURL: <span style="color: #0000ff">false      <br /></span>});</div>
</p>
<p>My solution to this was the following:</p>
<div class="vscode">$(<span style="color: #a31515">&quot;.tooltip-element&quot;</span>).tooltip({     <br />&#160;&#160;&#160; bodyHandler: <span style="color: #0000ff">function</span>() {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0000ff">var</span> result = $(<span style="color: #a31515">&quot;&lt;span/&gt;&quot;</span>).html(<span style="color: #a31515">&quot;some loading indicator&quot;</span>);     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $.<span style="color: #0000ff">get</span>(<span style="color: #a31515">&quot;/MyController/MyAction&quot;</span>, { id: <span style="color: #a31515">&quot;myid&quot;</span> }, <span style="color: #0000ff">function</span>(data) { result.html(data); }, <span style="color: #a31515">&quot;html&quot;</span>);     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0000ff">return</span> result;     <br />&#160;&#160;&#160; },     <br />&#160;&#160;&#160; showURL: <span style="color: #0000ff">false      <br /></span>});</div>
<p>I think that is pretty nifty! Of course the “some loading indicator” can be tweaked (e.g. use a <a href="http://ajaxload.info/">AJAX load indicator</a>). And this is of course <u>not</u> specific to ASP.NET MVC!</p>
<p><strong>UPDATE:</strong></p>
<p>There is a problem with this approach! The jQuery tooltip plugin used seems to invoke the bodyHandler callback directly “on mouse over”, ie. not after the delay. This means that although the tooltip displays nicely after the specified delay, the Ajax call was performed at T=0. What this effectively means is that if you eg. have a table where all values in column 1 have this Ajax-tooltip-thingy. If you then move the mouse in over a couple of the values each time you the cursor is over the value an Ajax call is executed. Not so good unless you want to utilize all resources in your server <img src='http://blog.veggerby.dk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Back to the drawing board…</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.veggerby.dk/2009/01/01/jquery-and-ajax-tooltips/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
