<?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>gunnarsteinn.com</title>
	<atom:link href="http://www.gunnarsteinn.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gunnarsteinn.com</link>
	<description></description>
	<lastBuildDate>Wed, 23 Sep 2009 22:47:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Easily cache results from Javascript functions</title>
		<link>http://www.gunnarsteinn.com/2009/09/easily-cache-results-from-javascript-functions/</link>
		<comments>http://www.gunnarsteinn.com/2009/09/easily-cache-results-from-javascript-functions/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 14:06:20 +0000</pubDate>
		<dc:creator>Gunnar Steinn</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.gunnarsteinn.com/?p=1147</guid>
		<description><![CDATA[UPDATE: Adrian Quark has a much safer approach for the cache in his comment. I guess I should have tested mine better with other functions than the computational ones I was doing  
A short Javascript function I made in a project to cache result from functions in Javascript. You can use this function where [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>UPDATE:</strong> Adrian Quark has a much safer approach for the cache in <a href="http://www.gunnarsteinn.com/2009/09/easily-cache-results-from-javascript-functions/#comment-7494">his comment</a>. I guess I should have tested mine better with other functions than the computational ones I was doing <img src='http://www.gunnarsteinn.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </em></p>
<p>A short Javascript function I made in a project to cache result from functions in Javascript. You can use this function where it makes sense to cache.</p>
<p><strong>The cache method</strong></p>
<pre class="brush: jscript;">cacheMethod.cache = {};

function cacheMethod(context, fn) {
	var funcName = fn.toString();
	funcName = funcName.substr('function '.length);
	funcName = funcName.substr(0, funcName.indexOf('('));

	context[funcName] = function() {

		if (cacheMethod.cache[funcName] == null)
			cacheMethod.cache[funcName] = {};

		// Create a unique key of parameters
		var key = Array.slice.call(arguments).join('|');
		if (cacheMethod.cache[funcName][key] == null)
			cacheMethod.cache[funcName][key] = fn.apply(this, arguments);

		return cacheMethod.cache[funcName][key];
	};
}</pre>
<p><strong>Usage</strong><br />
If we have a function like <code>isPrime</code> <a href="http://ejohn.org/apps/learn/#21">borrowed</a> from John Resig:</p>
<pre class="brush: jscript;">function isPrime( num ) {

  var prime = num != 1; // Everything but 1 can be prime
  for ( var i = 2; i &lt; num; i++ ) {
    if ( num % i == 0 ) {
      prime = false;
      break;
    }
  }

  return prime;
}</pre>
<p>we can make the isPrime function cache-able with a single line of:</p>
<pre class="brush: jscript;">cacheMethod(this, isPrime);</pre>
<p>No when we try to call the <code>isPrime</code> 100 times with a large prime number, this first round is calculated and all subsequent rounds use the cache. You can see the difference by removing the <code>cacheMethod</code> line.</p>
<pre class="brush: jscript;">for (var i=0; i&lt;100; i++)
		document.write(isPrime(58750291)+'&lt;br/&gt;');</pre>
<p>Full example here: <a href="/wp-content/cache.html">cache.html</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gunnarsteinn.com/2009/09/easily-cache-results-from-javascript-functions/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How custom HTML tags like Facebook Connect uses work</title>
		<link>http://www.gunnarsteinn.com/2009/09/how-custom-html-tags-like-facebook-connect-uses/</link>
		<comments>http://www.gunnarsteinn.com/2009/09/how-custom-html-tags-like-facebook-connect-uses/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 21:25:01 +0000</pubDate>
		<dc:creator>Gunnar Steinn</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.gunnarsteinn.com/?p=1106</guid>
		<description><![CDATA[While looking at the Facebook Connect API I was curious to see that it uses custom XML tags for it&#8217;s elements, for example &#60;fb:login-button/&#62;.  After the page is loaded the tag is replaced with &#60;a href="..." &#62;&#60;img src='...' /&#62;&#60;/a&#62; if the user is not logged in and a profile image and name if the [...]]]></description>
			<content:encoded><![CDATA[<p>While looking at the <a href="http://wiki.developers.facebook.com/index.php/Facebook_Connect">Facebook Connect</a> API I was curious to see that it uses custom XML tags for it&#8217;s elements, <a href="http://wiki.developers.facebook.com/index.php/Fb:login-button#Examples">for example</a> <code>&lt;fb:login-button/&gt;</code>.  After the page is loaded the tag is replaced with <code>&lt;a href="..." &gt;&lt;img src='...' /&gt;&lt;/a&gt;</code> if the user is not logged in and a profile image and name if the user is already logged in.</p>
<p>So I could better understand how this works I created a little sample were I extracted the important parts from the API. The sample takes in a instance of an User object and displays a profile picture and name of the user with the <code>gs:profile-image</code> and <code>gs:profile-text</code> tags.</p>
<p>First there is the main parser. It starts by defining the objects of the sample API in an array of <code>knownElements</code>. Then the <code>parseDomTree</code> function loops through all of the known elements and find all objects in the document that correspond to that tag.</p>
<p><strong>gsml.js</strong></p>
<pre class="brush: jscript;">var GS = {};
GS.GSML = {
	ProfileText : function(el) {
		var span = document.createElement('span');
		span.innerHTML = GS.user.username;
		el.innerHTML = &quot;&quot;;
		el.appendChild(span);

	},
	ProfileImage : function(el) {
		// Some logic to create the image from the logged in user
		var img = document.createElement('img');
		img.setAttribute('src', GS.user.img);
		img.setAttribute('onclick', el.getAttribute('clickhandler'));
		el.innerHTML = &quot;&quot;;
		el.appendChild(img);
	}
};

GS = {
	knownElements : [
		{key:'profile-image', class:GS.GSML.ProfileImage},
		{key:'profile-text',  class:GS.GSML.ProfileText}],
	user : null,

	init : function(user) {
		this.user = user;
	},

	parseDomTree : function(context) {

		// Loop through all known elements in our namespace
		for (var i=0; i&lt;this.knownElements.length; i++) {

			// Find all tags for current known element
			var elements = context.getElementsByTagName(&quot;GS:&quot;+ this.knownElements[i]['key']);
			for (var j=0; j&lt;elements.length; j++) {

				// Call the associated function to parse this tag
				this.knownElements[i]['class'](elements[j]);
			}
		}
	}
};</pre>
<p>In the HTML that uses the sample API there are four elements of interest; include of the sample javascript api, define the User object, tell the API to parse the DOM and add the custom API tags to the HTML body.</p>
<p><strong>thefile.html</strong></p>
<pre class="brush: xml; highlight: [19,20];">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:gs=&quot;http://www.gunnarsteinn.com/2009/gsml&quot;&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;gsml.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	var user = {
		username : 'Click Image',
		img : 'http://www.gunnarsteinn.com/wp-content/uploads/tf.jpg'
	};

	function imgClicked() {
		user.username = 'Tobias Funke';
		GS.parseDomTree(document);
	}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload=&quot;GS.init(user); GS.parseDomTree(document)&quot;&gt;

&lt;gs:profile-image clickhandler=&quot;imgClicked();&quot;&gt;&lt;/gs:profile-image&gt;
&lt;gs:profile-text&gt;&lt;/gs:profile-text&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>And the results:<br />
<iframe src="http://www.gunnarsteinn.com/fb/test.html" width="350px" height="200px" style="border: 0px"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gunnarsteinn.com/2009/09/how-custom-html-tags-like-facebook-connect-uses/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript magic</title>
		<link>http://www.gunnarsteinn.com/2009/09/javascript-magic/</link>
		<comments>http://www.gunnarsteinn.com/2009/09/javascript-magic/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 11:45:45 +0000</pubDate>
		<dc:creator>Gunnar Steinn</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.gunnarsteinn.com/?p=1098</guid>
		<description><![CDATA[Function.prototype.bind = function(){
  var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
  return function(){
    return fn.apply(object,
      args.concat(Array.prototype.slice.call(arguments)));
  };
};
If you want to spent an hour studying how this one function in JavaScript works there is an excellent demonstration at John Resig&#8217;s blog.
The code uses [...]]]></description>
			<content:encoded><![CDATA[<pre class="brush: jscript;">Function.prototype.bind = function(){
  var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
  return function(){
    return fn.apply(object,
      args.concat(Array.prototype.slice.call(arguments)));
  };
};</pre>
<p>If you want to spent an hour studying how this one function in JavaScript works there is an excellent demonstration at <a href="http://ejohn.org/apps/learn/#1">John Resig&#8217;s blog</a>.</p>
<p>The code uses three types of method invocation in just a couple of lines (shift(), apply and call). Isn&#8217;t JavaScript too complex?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gunnarsteinn.com/2009/09/javascript-magic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blogg</title>
		<link>http://www.gunnarsteinn.com/2009/09/blogg-4/</link>
		<comments>http://www.gunnarsteinn.com/2009/09/blogg-4/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 09:58:18 +0000</pubDate>
		<dc:creator>Gunnar Steinn</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.gunnarsteinn.com/?p=1094</guid>
		<description><![CDATA[Ég finn alltaf fyrir mikill bloggþörf þó ég hafi margoft byrjað og nákvæmlega jafn oft hætt (ef við teljum þessa færslu ekki með, ennþá).
Klassíska vandamálið, þegar það er nóg að blogga um hef ég ekki tíma til að blogga um það, og þess á milli, þegar ég hef nægan tíma, hef ég ekkert að blogga [...]]]></description>
			<content:encoded><![CDATA[<p>Ég finn alltaf fyrir mikill bloggþörf þó ég hafi margoft byrjað og nákvæmlega jafn oft hætt (ef við teljum þessa færslu ekki með, ennþá).</p>
<p>Klassíska vandamálið, þegar það er nóg að blogga um hef ég ekki tíma til að blogga um það, og þess á milli, þegar ég hef nægan tíma, hef ég ekkert að blogga um&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gunnarsteinn.com/2009/09/blogg-4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
