<?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>Death of a Gremmie &#187; javascript</title>
	<atom:link href="http://deathofagremmie.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://deathofagremmie.com</link>
	<description>by Brian Neal</description>
	<lastBuildDate>Wed, 14 Jul 2010 02:31:19 +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 UI Autocomplete Widget &amp; Caching</title>
		<link>http://deathofagremmie.com/2010/04/03/jquery-ui-autocomplete-widget-caching/</link>
		<comments>http://deathofagremmie.com/2010/04/03/jquery-ui-autocomplete-widget-caching/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 21:30:54 +0000</pubDate>
		<dc:creator>gremmie</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[autocomplete]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery-ui]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://deathofagremmie.com/?p=349</guid>
		<description><![CDATA[I upgraded to the awesome new 1.4 version of jQuery recently in preparation for deploying another beta version of my site. Unfortunately the jquery-autocomplete plugin by Jörn Zaefferer doesn&#8217;t work with this new version. However, the equally awesome jQuery user interface library, jQuery UI, now has a new autocomplete widget that is based upon Jörn&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I upgraded to the awesome new 1.4 version of <a href="http://jquery.com/">jQuery</a> recently in preparation for deploying another beta version of my site. Unfortunately the <a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/">jquery-autocomplete plugin by Jörn Zaefferer</a> doesn&#8217;t work with this new version. However, the equally awesome jQuery user interface library, <a href="http://jqueryui.com/">jQuery UI</a>, now has a new <a href="http://jqueryui.com/demos/autocomplete/">autocomplete widget</a> that is based upon Jörn&#8217;s design. The API isn&#8217;t quite the same, but after some reading you&#8217;ll find that most of the functionality is still there. However the new UI widget does not support caching requests, leaving that to the user to implement. This kind of surprised me, as I would think that would be a commonly requested feature. But perhaps the UI folks felt that everyone would have different caching requirements. There is <a href="http://jqueryui.com/demos/autocomplete/#remote-with-cache">one example on the UI site of how to cache</a>, but curiously it only caches the last request, which doesn&#8217;t seem all that useful to me.</p>
<p>In any event, after reading the documentation, it wasn&#8217;t hard to implement caching for my purposes. The following code simply caches the last 16 requests. Once the cache is full, it is flushed completely and ready to be filled again. Simple, crude, but hopefully somewhat effective.  I&#8217;m not a Javascript expert, but this will work for me. I hope someone else finds it useful and can use it as a starting point for their use-case.</p>
<pre>
<pre class="brush: javascript">
$(function() {
    var cache = {};
    var cacheSize = 0;
    $(&quot;#id_of_your_text_box&quot;).autocomplete({
        delay: 400,
        minLength: 2,
        source: function(request, response) {
            if (cache[request.term]) {
               response(cache[request.term]);
               return;
            }
            $.ajax({
                url: &quot;/your/url/goes/here/&quot;,
                type: &quot;GET&quot;,
                data: {
                    q: request.term,
                    limit: 10
                },
                dataType: &quot;json&quot;,
                success: function(data, textStatus) {
                    if (cacheSize &gt;= 16) {
                       cache = {};
                       cacheSize = 0;
                    }
                    cache[request.term] = data;
                    ++cacheSize;
                    response(data);
                },
                error: function(xhr, textStatus, ex) {
                    alert(&#039;Oops, an error occurred. &#039; + xhr.statusText + &#039; - &#039; +
                      xhr.responseText);
                }
            });
        }
    });
});
</pre>
</pre>
<p>The key to making this to work is to provide your own source function to the autocomplete widget. The widget will call this function when it needs data. Inside the function we simply check to see if request.term is already in our cache. If it is, then we simply return the cached result via the response callback function. If not, we have to make an ajax call to the server to retrieve the data.  When the ajax call completes, we store the result in our cache and then return it to the widget.</p>
<p>Again, this code simply stores the last 16 requests. When we run out of room we simply empty the cache and start over. A more sophisticated algorithm could use a different strategy like removing only the first item put in the cache or even the &#8220;least recently used&#8221; cache item. Break out your college computer science textbook on caching strategies and go nuts here.</p>
<p>The other feature this code doesn&#8217;t do is provide matching capabilities. For example, if the user has typed &#8220;foo&#8221; in the input box, this code will not return results for &#8220;bar foo&#8221;. If you need this, I suggest looking at the caching example on the UI site, or even at Jörn Zaefferer&#8217;s source code.</p>
]]></content:encoded>
			<wfw:commentRss>http://deathofagremmie.com/2010/04/03/jquery-ui-autocomplete-widget-caching/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Firefox, Ubuntu, NVIDIA, and Slow Javascript</title>
		<link>http://deathofagremmie.com/2009/05/07/firefox-ubuntu-nvidia-javascript/</link>
		<comments>http://deathofagremmie.com/2009/05/07/firefox-ubuntu-nvidia-javascript/#comments</comments>
		<pubDate>Thu, 07 May 2009 23:38:22 +0000</pubDate>
		<dc:creator>gremmie</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[nvidia]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://deathofagremmie.com/?p=249</guid>
		<description><![CDATA[I noticed that some web pages that make use of Javascript were acting very slow in Firefox on my Ubuntu 8.04 LTS powered Dell XPS M1530 laptop that has an NVIDIA graphics card. Apparently a lot of other people have too. I finally got annoyed enough to research my options, and a work-around posted in [...]]]></description>
			<content:encoded><![CDATA[<p>I noticed that some web pages that make use of Javascript were acting very slow in <a href="http://www.mozilla.com/firefox/">Firefox</a> on my <a href="http://www.ubuntu.com/">Ubuntu</a> 8.04 LTS powered Dell XPS M1530 laptop that has an <a href="http://www.nvidia.com/">NVIDIA</a> graphics card. Apparently <a href="https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/223238">a lot of other people have too</a>. I finally got annoyed enough to research my options, and a work-around posted in the referenced bug tracker did the trick for me. First I had to do a &#8220;apt-get install nvidia-settings&#8221; to install a program that lets you tweak the NVIDIA driver settings. Then you need to tweak one setting as follows:</p>
<pre>$ nvidia-settings -q InitialPixmapPlacement
$ nvidia-settings -a InitialPixmapPlacement=2</pre>
<p>The first command shows you the current value of the setting InitialPixmapPlacement. On my system, this was a 1. I wrote this down in case I need to switch it back on a future release. On the second line, you jam that setting to a 2. And voila, everything suddenly ran a lot smoother.</p>
<p><em>Postscript: </em>Although this did seem to help with some issues, it still isn&#8217;t completely resolved. Aargh.</p>
<p><em>Update 12 November 2009:</em> Changing the Gnome desktop theme to Clearlooks from Human made a noticeable difference. It isn&#8217;t perfect yet but it really helped.</p>
<p><em>Update 2 May 2010: </em>This problem is resolved in Ubuntu 10.04 if you install the nvidia-current package. Use the &#8220;Hardware Drivers&#8221; program (found under the menu System / Administration) to select &#8220;NVIDIA accelerated graphics driver (version current) [Recommended]&#8220;. Restart your computer and finally enjoy smooth Javascript.</p>
]]></content:encoded>
			<wfw:commentRss>http://deathofagremmie.com/2009/05/07/firefox-ubuntu-nvidia-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
