<?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>Velu&#039;s Blog</title>
	<atom:link href="http://gdmig-ksankaran.com/wp/feed/" rel="self" type="application/rss+xml" />
	<link>http://gdmig-ksankaran.com/wp</link>
	<description>Problems &#38; Solutions. or just how-to.</description>
	<lastBuildDate>Thu, 03 Oct 2013 19:48:32 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.8.36</generator>
	<item>
		<title>Website Offline Processing / Speed Up Page Load</title>
		<link>http://gdmig-ksankaran.com/wp/2013/10/03/website-offline-processing-speed-page-load/</link>
		<comments>http://gdmig-ksankaran.com/wp/2013/10/03/website-offline-processing-speed-page-load/#comments</comments>
		<pubDate>Thu, 03 Oct 2013 19:47:27 +0000</pubDate>
		<dc:creator><![CDATA[Velu Sankaran]]></dc:creator>
				<category><![CDATA[Application Cache]]></category>
		<category><![CDATA[Browser]]></category>
		<category><![CDATA[Cross Domain Communication]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://ksankaran.com/wp/?p=335</guid>
		<description><![CDATA[<div id="fb-root"></div>Introduction This article assumes that the user is aware of the process by which a webpage renders on a browser, how the resources are fetched, etc. The document also assumes that the user have basic understanding of browser caches and cache headers. This article provides the basic information on using appcache and a demo page&#8230; <a href="http://gdmig-ksankaran.com/wp/2013/10/03/website-offline-processing-speed-page-load/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This article assumes that the user is aware of the process by which a webpage renders on a browser, how the resources are fetched, etc. The document also assumes that the user have basic understanding of browser caches and cache headers. This article provides the basic information on using appcache and a demo page to explain its benefits.</p>
<h2>AppCache vs Browser Cache</h2>
<p>When a page is requested on the browser without appcache, there will be a sequence of network requests sent from the browser to the server. The first request is the actual page (raw html) you wanted to load. Then browser reads the html and process them line by line. Every script tag, css link tag or any other resources on the page are requested sequentially. Each of this requests may be cached in the browser and this forms the browser cache. I said &#8220;may be&#8221; because the rule for a resource getting cached is: resource headers sent from your server. See example below:<br />
<a class="thumbnail" target="_blank" href="http://ksankaran.com/wp/wp-content/uploads/2013/07/cachecontrolexample.jpg"><img src="http://ksankaran.com/wp/wp-content/uploads/2013/07/cachecontrolexample.jpg" alt="cachecontrolexample" width="328" height="132" class="aligncenter size-full wp-image-336" /></a></p>
<p>So, it is evident that a browser cannot cache these items unless and otherwise, we visit the page and also depends on headers received for individual resources. But, what if you want to cache the extra few items that needed to display next page? What if, your website need to work offline and sync only when needed or when the user gets online? What if, you have a website which have separate layer for presentation/logic and separate layer for data and like to store all the presentation/logic offline? The answer is simple and straightforward: AppCache.<br />
Now, let&#8217;s not get excited and convert all our websites to webapp. HTML5 AppCache is designed for offline webapps &#8211; make no mistake. A simple error such as app-caching a dynamic data would result in rendering outdated website. So, it takes time to understand AppCache and make sure you appcache the right set of resources.</p>
<h2>How to do it?</h2>
<p>Well, its pretty simple. Write a manifest file describing the items to be cached, network behavior, fallback, etc and mention the manifest in the HTML. That&#8217;s it.<br />
A sample would be below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;">CACHE MANIFEST
<span style="color: #666666; font-style: italic;"># ======================================
</span><span style="color: #666666; font-style: italic;"># AppCache Manifest - version 0.1
</span><span style="color: #666666; font-style: italic;"># ======================================
</span>CACHE<span style="color: #339933;">:</span>
<span style="color: #666666; font-style: italic;">#JS files
</span><span style="color: #666666; font-style: italic;">#CSS files
</span><span style="color: #666666; font-style: italic;">#Images
</span>NETWORK<span style="color: #339933;">:</span>
<span style="color: #666666; font-style: italic;"># Resources that require the user to be online.
</span>FALLBACK<span style="color: #339933;">:</span>
<span style="color: #666666; font-style: italic;"># static.html will be served if main.py is inaccessible
</span><span style="color: #339933;">/</span>main<span style="color: #339933;">.</span>py <span style="color: #339933;">/</span>static<span style="color: #339933;">.</span>html
<span style="color: #666666; font-style: italic;"># offline.jpg will be served in place of all images in images/large/
</span>images<span style="color: #339933;">/</span>large<span style="color: #339933;">/</span> images<span style="color: #339933;">/</span>offline<span style="color: #339933;">.</span>jpg
<span style="color: #666666; font-style: italic;"># offline.html will be served in place of all other .html files
</span><span style="color: #339933;">*.</span>html <span style="color: #339933;">/</span>offline<span style="color: #339933;">.</span>html</pre></td></tr></table></div>

<p>NOTE: The CACHE MANIFEST string should be the first line and is required.<br />
CACHE:<br />
This is the default section for entries. Files listed under this header (or immediately after the CACHE MANIFEST) will be explicitly cached after they&#8217;re downloaded for the first time.<br />
NETWORK:<br />
Files listed under this section are white-listed resources that require a connection to the server. All requests to these resources bypass the cache, even if the user is offline. Wildcards may be used.<br />
FALLBACK:<br />
An optional section specifying fallback pages if a resource is inaccessible. The first URI is the resource, the second is the fallback. Both URIs must be relative and from the same origin as the manifest file. Wildcards may be used.<br />
Now add the manifest to the html file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;html manifest=&quot;example.appcache&quot;&gt;
  ...
&lt;/html&gt;</pre></td></tr></table></div>

<h2>Cache Update</h2>
<p>The tricky part is updating the cache items. When the content on the server is changed, the browser will not know the change and will continue to use the outdated data on the webpage. The appcache manifest should be fetched everytime by the browser to determine the items that has to be refreshed. Hence, it is VERY IMPORTANT to make sure appcache manifest is never cached by the browser or at least not for a long time. For that, it is important that the headers sent by the webserver for the manifest also includes the expiry time. It is also possible to update the appcache via the javascript like</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> appCache <span style="color: #339933;">=</span> window.<span style="color: #660066;">applicationCache</span><span style="color: #339933;">;</span>
appCache.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Attempt to update the user's cache.</span>
...
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>appCache.<span style="color: #660066;">status</span> <span style="color: #339933;">==</span> window.<span style="color: #660066;">applicationCache</span>.<span style="color: #660066;">UPDATEREADY</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  appCache.<span style="color: #660066;">swapCache</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// The fetch was successful, swap in the new cache.</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It is also possible to clear the cache when the user clears the same in his/her browser. However, that is not a valid workflow for updating the cache.</p>
<h2>The Gotchas</h2>
<p>The appcache also caches the base page. This means, if you later change the base page to include an additional library or UI, it will not be fetched from server unless there is a manifest file update. In order to bypass this functionality, it is ok to include the appcache in a temporary html and include the temp html as an iframe in the base page (See demo link below). By this way, you still cache the manifest items but the base page will not be cached by the appcache unless specified in the manifest.</p>
<h2>Demo <a href="http://demo.ksankaran.com/appcache/">link</a></h2>
<h2>Conclusion</h2>
<p>We saw the basic differences between appcache and browser cache with a basic example to include app caches. The example link also shows how to include the appcache by not affecting the cache of the base page by providing manifest in a temp html and loading the same in a iframe.
<div class="ptl_div"><script>
					//<![CDATA[
					document.write('<g:plusone size="standard" annotation="none" href="http://gdmig-ksankaran.com/wp/2013/10/03/website-offline-processing-speed-page-load/"></g:plusone>');
				        //]]&gt;
					</script><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://gdmig-ksankaran.com/wp/2013/10/03/website-offline-processing-speed-page-load/" data-text="Website Offline Processing / Speed Up Page Load" data-count="vertical">Tweet</a>
<div class="ptl_fb"><script>
							//<![CDATA[
							document.write('<fb:like href="http://gdmig-ksankaran.com/wp/2013/10/03/website-offline-processing-speed-page-load/" show_faces="true" width="225"  action="like" font="" colorscheme="light"></fb:like>');
							//]]&gt;
							</script></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://gdmig-ksankaran.com/wp/2013/10/03/website-offline-processing-speed-page-load/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Generate Dynamic JavaScript</title>
		<link>http://gdmig-ksankaran.com/wp/2013/06/13/generate-dynamic-javascript/</link>
		<comments>http://gdmig-ksankaran.com/wp/2013/06/13/generate-dynamic-javascript/#comments</comments>
		<pubDate>Thu, 13 Jun 2013 03:09:29 +0000</pubDate>
		<dc:creator><![CDATA[Velu Sankaran]]></dc:creator>
				<category><![CDATA[Dynamic JS Generation]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[generation]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://ksankaran.com/wp/?p=291</guid>
		<description><![CDATA[Introduction The article explains how to generate dynamic script from a backend language, such as PHP in this case. There are many reasons to choose a dynamic script over generation of JSON data that client can write script on such as 1) Restrict the client from manipulating data. 2) just provide a helper API that&#8230; <a href="http://gdmig-ksankaran.com/wp/2013/06/13/generate-dynamic-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>The article explains how to generate dynamic script from a backend language, such as PHP in this case. There are many reasons to choose a dynamic script over generation of JSON data that client can write script on such as 1) Restrict the client from manipulating data. 2) just provide a helper API that would just do the job. 3) Formation of feasible JS data structure is not possible.</p>
<h2>Prerequisites</h2>
<p>The article assumes that user knows PHP and JavaScript basics. We will be trying to read the .properties file from php, parse the configuration, understands what user wants to, prints the logic in JS and load the same in HTML to test the logic. I would also recommend you have some basic knowledge on recursive method call techniques as well. To know more on recursion, <a target="_blank" href="http://en.wikipedia.org/wiki/Recursion_(computer_science)">please read here</a>.</p>
<h2>Defining a Problem Statement</h2>
<p>Let&#8217;s try and define the problem we are trying to solve. Consider a use-case where you have a properties file on the server and we have to use this file to show a message in the front end. Well, it is not a straightforward map of key1 to value1. Assume that, it is complex configuration of three level key to a value and each level can have wild card character &#8216;?&#8217; to accept any value at that level or contain range at every level. Example: &#8220;28|71-100|?=text3&#8243; -> if level1 value is 28 and level2 value is between 71 and 100 and level3 at any value, then the text to be used is &#8220;text3&#8243;.</p>
<h2>Parse Properties and Generate Script</h2>
<p>Assume that our file has one configuration per line and the first step is to understand the properties file and getting ready for generation.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$key1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'t'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$key2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'w'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$key3</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'t'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> get_object_from_params<span style="color: #009900;">&#40;</span><span style="color: #000088;">$paramStr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$assignValue</span><span style="color: #339933;">,</span> <span style="color: #000088;">$label</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$paramStr</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'?'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$paramObj</span> <span style="color: #339933;">=</span> <span style="color: #990000;">Array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #000088;">$label</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$assignValue</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$ranges</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/-/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paramStr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ranges</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$paramObj</span> <span style="color: #339933;">=</span> <span style="color: #990000;">Array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">&quot;&gt;&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ranges</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;&lt;&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ranges</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                <span style="color: #000088;">$label</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$assignValue</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$paramObj</span> <span style="color: #339933;">=</span> <span style="color: #990000;">Array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">&quot;==&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ranges</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                <span style="color: #000088;">$label</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$assignValue</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$paramObj</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> parse_icon_config<span style="color: #009900;">&#40;</span><span style="color: #000088;">$txtProperties</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$lines</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #000099; font-weight: bold;">\n</span>/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$txtProperties</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$key1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key3</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$lineparts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/=/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$textvalue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lineparts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$configparts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/\|/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lineparts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$configparts</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$windSpeed</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$configparts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$configparts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$iconCode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$configparts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$windObj</span> <span style="color: #339933;">=</span> get_object_from_params<span style="color: #009900;">&#40;</span><span style="color: #000088;">$windSpeed</span><span style="color: #339933;">,</span> <span style="color: #000088;">$textvalue</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$tempObj</span> <span style="color: #339933;">=</span> get_object_from_params<span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$windObj</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$iconObj</span> <span style="color: #339933;">=</span> get_object_from_params<span style="color: #009900;">&#40;</span><span style="color: #000088;">$iconCode</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tempObj</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #000088;">$iconObj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$properties</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config/iconconfig.properties'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$propertiesObj</span> <span style="color: #339933;">=</span> parse_icon_config<span style="color: #009900;">&#40;</span><span style="color: #000088;">$properties</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above snippet parses the configuration file and generates array of config objects &#8211; one per each line. Since there is a three level key and each level parsing is almost the same, lets write a recursive method to generate the script.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> recursive_print<span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #339933;">,</span> <span style="color: #000088;">$label</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$key3</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'return \''</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$obj</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'\';'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'=='</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'&gt;'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'&lt;'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$nextLabel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$nextObj</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;==&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'if('</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$label</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'=='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$obj</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'=='</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'){'</span> <span style="color: #339933;">.</span> recursive_print<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextObj</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nextLabel</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'}'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'if('</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$label</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$obj</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'&gt;'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' &amp;&amp; '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$label</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$obj</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'&lt;'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'){'</span> <span style="color: #339933;">.</span> recursive_print<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextObj</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nextLabel</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'}'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'if(true){'</span> <span style="color: #339933;">.</span> recursive_print<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextObj</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nextLabel</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'}'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> get_function_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$iconCodesConfig</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$key0</span><span style="color: #339933;">,</span><span style="color: #000088;">$key1</span><span style="color: #339933;">,</span><span style="color: #000088;">$key2</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$functionCode</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iconCodesConfig</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$iconConfig</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$functionCode</span> <span style="color: #339933;">.=</span> recursive_print<span style="color: #009900;">&#40;</span><span style="color: #000088;">$iconConfig</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'window.getDynamicText = function('</span><span style="color: #339933;">.</span><span style="color: #000088;">$key0</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">.</span><span style="color: #000088;">$key1</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">.</span> <span style="color: #000088;">$key2</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'){'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$functionCode</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'return \'not found. some default value.\'}'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>At each level, we analyze the data and print the appropriate conditionals in the JavaScript and then call the same method again with the next level until we reach the last level. </p>
<p>Our javascript generation is ready, when the PHP is requested, this is what we get:<br />
<a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/06/jsgenresponse.jpg" target="_blank"><img src="http://ksankaran.com/wp/wp-content/uploads/2013/06/jsgenresponse.jpg" alt="jsgenresponse" width="761" height="102" class="aligncenter size-full wp-image-307" /></a></p>
<p>Now, using it in our HTML is quite easy. As many other APIs out there, it loads in the window namespace and waits for us to call.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;jsgen.php&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span>
<span style="color: #339933;">....</span>
<span style="color: #339933;">&lt;</span>button <span style="color: #339933;">...</span> onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;validateAndGet()&quot;</span><span style="color: #339933;">&gt;</span>Get Dynamic Text<span style="color: #339933;">&lt;/</span>button<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h3>Here is a demo <a target="_blank" href="http://demo.ksankaran.com/jsgenerator/">link</a>.</h3>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/06/demosnap.jpg" target="_blank"><img src="http://ksankaran.com/wp/wp-content/uploads/2013/06/demosnap.jpg" alt="demosnap" width="1015" height="547" class="aligncenter size-full wp-image-319" /></a></p>
<h2>Conclusion</h2>
<p>What did we just do: we discussed a set of use cases when we need a dynamic script rather than just data, picked up a particular scenario and worked on the same to generate a dynamic javascript using PHP and recursion techniques.
<div class="ptl_div"><script>
					//<![CDATA[
					document.write('<g:plusone size="standard" annotation="none" href="http://gdmig-ksankaran.com/wp/2013/06/13/generate-dynamic-javascript/"></g:plusone>');
				        //]]&gt;
					</script><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://gdmig-ksankaran.com/wp/2013/06/13/generate-dynamic-javascript/" data-text="Generate Dynamic JavaScript" data-count="vertical">Tweet</a>
<div class="ptl_fb"><script>
							//<![CDATA[
							document.write('<fb:like href="http://gdmig-ksankaran.com/wp/2013/06/13/generate-dynamic-javascript/" show_faces="true" width="225"  action="like" font="" colorscheme="light"></fb:like>');
							//]]&gt;
							</script></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://gdmig-ksankaran.com/wp/2013/06/13/generate-dynamic-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use EXIF Metadata to extract GPS Info</title>
		<link>http://gdmig-ksankaran.com/wp/2013/06/11/exif-metadata-extract-gps-info/</link>
		<comments>http://gdmig-ksankaran.com/wp/2013/06/11/exif-metadata-extract-gps-info/#comments</comments>
		<pubDate>Tue, 11 Jun 2013 22:56:09 +0000</pubDate>
		<dc:creator><![CDATA[Velu Sankaran]]></dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[exif]]></category>
		<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[metadata]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://ksankaran.com/wp/?p=260</guid>
		<description><![CDATA[Introduction The pictures we take every day with our mobile or any camera that supports location tagging, contain information on location where the picture was taken. This can be extracted using wide variety of languages like php, javascript, java, etc but today we will be seeing a working model with php. Prerequisites The article assumes&#8230; <a href="http://gdmig-ksankaran.com/wp/2013/06/11/exif-metadata-extract-gps-info/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>The pictures we take every day with our mobile or any camera that supports location tagging, contain information on location where the picture was taken. This can be extracted using wide variety of languages like php, javascript, java, etc but today we will be seeing a working model with php.</p>
<h2>Prerequisites</h2>
<p>The article assumes that the user knows PHP and basic Javascript as well. I will be using jQuery to perform asynchronous file uploads and in case if you don&#8217;t know, please read it all <a href="http://api.jquery.com/jQuery.post/" target="_blank">here</a> on jQuery.</p>
<h2>EXIF Metadata</h2>
<p>DEMO: link provided at the end of this article.<br />
EXIF (exchangeable image file) data is a record, showing settings used to take a photograph. This data is recorded into the actual image file. Therefore each photograph has its own unique data. EXIF data stores information like camera model, exposure, aperture, ISO, what camera mode was used and whether or not a flash fired and location information as well. </p>
<p>The image below is an example of what EXIF data looks like. This information differs depending on what EXIF reader is used to see the metadata. The following sample picture is taken with my iPhone 4S and hence you can see all that information here.<br />
<a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/06/sampleexifdata.jpg" target="_blank"><img src="http://ksankaran.com/wp/wp-content/uploads/2013/06/sampleexifdata-1024x494.jpg" alt="sampleexifdata" width="770" height="371" class="alignright size-large wp-image-266" /></a></p>
<h2>Reading EXIF Metadata from PHP</h2>
<p>Reading EXIF metadata from PHP is quite easy and straightforward. However, in our case, we give the option for the user to upload the image or use the sample we have in our server. The HTML page bearing the google maps waits for the response from PHP on the exif metadata. Once the data is received, we try to plot the same on the map.</p>
<p>The following code tries to upload the file asynchronously to server to read the image file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> formData <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> FormData<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'upload.php'</span><span style="color: #339933;">,</span>  <span style="color: #006600; font-style: italic;">//server script to process data</span>
    type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span>
    success<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> textStatus<span style="color: #339933;">,</span> jqXHR<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ...
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    error<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>jqXHR<span style="color: #339933;">,</span> textStatus<span style="color: #339933;">,</span> errorThrown<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ...
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #006600; font-style: italic;">// Form data</span>
    data<span style="color: #339933;">:</span> formData<span style="color: #339933;">,</span>
    <span style="color: #006600; font-style: italic;">//Options to tell JQuery not to process data or worry about content-type</span>
    cache<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    contentType<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    processData<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, on the server side, process the image and sends back the response in JSON format &#8211; which is quite easy to display data at different places.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$currFileLocation</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;tmp_name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$imagedata</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$currFileLocation</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$base64image</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$imagedata</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$exif</span> <span style="color: #339933;">=</span> <span style="color: #990000;">exif_read_data</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$currFileLocation</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GPSLongitude'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exif</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GPSLatitude'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exif</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$lng</span> <span style="color: #339933;">=</span> getGps<span style="color: #009900;">&#40;</span><span style="color: #000088;">$exif</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;GPSLongitude&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exif</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'GPSLongitudeRef'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$lat</span> <span style="color: #339933;">=</span> getGps<span style="color: #009900;">&#40;</span><span style="color: #000088;">$exif</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;GPSLatitude&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exif</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'GPSLatitudeRef'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;status&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;success&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;reason&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;gps&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">&quot;lat&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$lat</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;lng&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$lng</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;img&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$base64image</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;status&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;fail&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;reason&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;The uploaded picture does not have GPS information.&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;exif&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$exif</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>getGps is a utility method written by a developer and was posted at <a href="http://stackoverflow.com/a/2572991/842065" target="_blank">Stack Overflow</a>. The code is below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getGps<span style="color: #009900;">&#40;</span><span style="color: #000088;">$exifCoord</span><span style="color: #339933;">,</span> <span style="color: #000088;">$hemi</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$degrees</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$exifCoord</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> ? gps2Num<span style="color: #009900;">&#40;</span><span style="color: #000088;">$exifCoord</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$minutes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$exifCoord</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> ? gps2Num<span style="color: #009900;">&#40;</span><span style="color: #000088;">$exifCoord</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$seconds</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$exifCoord</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2</span> ? gps2Num<span style="color: #009900;">&#40;</span><span style="color: #000088;">$exifCoord</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$flip</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$hemi</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'W'</span> or <span style="color: #000088;">$hemi</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'S'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$flip</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$degrees</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$minutes</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$seconds</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">3600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> gps2Num<span style="color: #009900;">&#40;</span><span style="color: #000088;">$coordPart</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$parts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$coordPart</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">floatval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #990000;">floatval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Getting the response and plotting to map is quite easy. The map is already loaded on the page and points to a default location. We are going to alter our center point as well as add marker with Info Window.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">moveCenterAndPlaceMarker <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> geo <span style="color: #339933;">=</span> data.<span style="color: #660066;">gps</span><span style="color: #339933;">;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;moving marker: &quot;</span> <span style="color: #339933;">+</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>geo<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> latLng <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLng</span><span style="color: #009900;">&#40;</span>geo.<span style="color: #660066;">lat</span><span style="color: #339933;">,</span> geo.<span style="color: #660066;">lng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>latLng<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    map.<span style="color: #660066;">setZoom</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> contentString <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;div style=&quot;width: 200px; height: 150px;&quot;&gt;The picture you took at this place is:&lt;img width=&quot;120&quot; height=&quot;120&quot; src=&quot;data:image/jpeg;base64,'</span><span style="color: #339933;">+</span>data.<span style="color: #660066;">img</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;&lt;/div&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Marker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        map<span style="color: #339933;">:</span> map<span style="color: #339933;">,</span>
        position<span style="color: #339933;">:</span> latLng<span style="color: #339933;">,</span>
        title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'The picture was taken right here! (I guess...)'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">event</span>.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        infowindow.<span style="color: #660066;">setContent</span><span style="color: #009900;">&#40;</span>contentString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        infowindow.<span style="color: #660066;">open</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> marker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>One thing you might not have noticed is the conversion of image we uploaded to base64 format. That format is returned and is loaded inside the google info window showing the image inside the google&#8217;s popup.<br />
As you can see, the following picture is taken by me at the airport and the picture&#8217;s GPS co-ordinates is bang on the target.<br />
<a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/06/samplegpsdata.jpg" target="_blank"><img src="http://ksankaran.com/wp/wp-content/uploads/2013/06/samplegpsdata.jpg" alt="samplegpsdata" width="956" height="459" class="alignright size-full wp-image-279" /></a></p>
<p>DEMO: link <a href="http://demo.ksankaran.com/exif/" target="_blank">here</a>.</p>
<h2>Conclusion</h2>
<p>What did we just see: We saw the basics of EXIF metadata, how to do that in PHP, how to upload a file asynchronously in jQuery and parse EXIF data and get back. Also, we saw loading non-url images inside google info window.
<div class="ptl_div"><script>
					//<![CDATA[
					document.write('<g:plusone size="standard" annotation="none" href="http://gdmig-ksankaran.com/wp/2013/06/11/exif-metadata-extract-gps-info/"></g:plusone>');
				        //]]&gt;
					</script><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://gdmig-ksankaran.com/wp/2013/06/11/exif-metadata-extract-gps-info/" data-text="Use EXIF Metadata to extract GPS Info" data-count="vertical">Tweet</a>
<div class="ptl_fb"><script>
							//<![CDATA[
							document.write('<fb:like href="http://gdmig-ksankaran.com/wp/2013/06/11/exif-metadata-extract-gps-info/" show_faces="true" width="225"  action="like" font="" colorscheme="light"></fb:like>');
							//]]&gt;
							</script></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://gdmig-ksankaran.com/wp/2013/06/11/exif-metadata-extract-gps-info/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cache JSON(P) calls client side</title>
		<link>http://gdmig-ksankaran.com/wp/2013/06/07/cache-jsonp-calls-client-side/</link>
		<comments>http://gdmig-ksankaran.com/wp/2013/06/07/cache-jsonp-calls-client-side/#comments</comments>
		<pubDate>Fri, 07 Jun 2013 19:55:46 +0000</pubDate>
		<dc:creator><![CDATA[Velu Sankaran]]></dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JScache]]></category>
		<category><![CDATA[Promise]]></category>
		<category><![CDATA[angularJS]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[clientside]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[jsonp]]></category>

		<guid isPermaLink="false">http://ksankaran.com/wp/?p=236</guid>
		<description><![CDATA[Prerequisites The article assumes the reader knows the basics of AngularJS. The article shows how the cache logic can be written in JavaScript but the UI render is done using AngularJS. A non-angular reader can still choose to continue reading through the article and can get the logic bits from the code. I leave it&#8230; <a href="http://gdmig-ksankaran.com/wp/2013/06/07/cache-jsonp-calls-client-side/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2>Prerequisites</h2>
<p>The article assumes the reader knows the basics of AngularJS. The article shows how the cache logic can be written in JavaScript but the UI render is done using AngularJS. A non-angular reader can still choose to continue reading through the article and can get the logic bits from the code. I leave it up to you.</p>
<h2>Introduction</h2>
<p>In real world, where we have CMS (Content Management System) to assemble our page with modules and each module function independently. The modules are developed by independent developers and the page is assembled by, probably, a different user altogether. AJAX calls are always meant to enhance user experience, but given the fact the each module function independently, there seems to a duplication of AJAX calls made on the page, probably by different modules. This post shows a way, how to cache such AJAX calls with the help of jQuery promise and a JavaScript object.</p>
<h2>jQuery Promises</h2>
<p>As name suggests, jQuery Promise is a literal promise made by jQuery that a call will be made on the object after its completion. The object is just like an JavaScript object and can be passed around like a ball to any method you want and any number of times you want. For more, read <a href="http://api.jquery.com/promise/" target="_blank">here</a>.</p>
<h2>Details</h2>
<p>Now that you have an idea of what we are going to do, let me take you through each step of the process.</p>
<h3>Constructing an deferred object cache</h3>
<p>I will try not to include AngularJS code in the sample but in some places it is unavoidable. Assuming that &#8220;command&#8221; is the part of the URL and &#8220;params&#8221; are the parameter key-value map, here is a snapshot of constructing a cache map.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> cacheStorage <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> getCacheKey<span style="color: #009900;">&#40;</span>command<span style="color: #339933;">,</span> params<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> paramStr <span style="color: #339933;">=</span> command <span style="color: #339933;">+</span> <span style="color: #3366CC;">'-'</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>params<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> keys <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> key <span style="color: #000066; font-weight: bold;">in</span> params<span style="color: #009900;">&#41;</span> keys.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">var</span> sortedKeys <span style="color: #339933;">=</span> keys.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> count<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> count <span style="color: #339933;">&lt;</span> sortedKeys.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> count<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">var</span> sKey <span style="color: #339933;">=</span> sortedKeys<span style="color: #009900;">&#91;</span>count<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            paramStr <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>sKey <span style="color: #339933;">+</span> <span style="color: #3366CC;">'-'</span> <span style="color: #339933;">+</span> params<span style="color: #009900;">&#91;</span>sKey<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>count <span style="color: #339933;">&lt;</span> sortedKeys.<span style="color: #660066;">length</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'-'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> paramStr<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">get</span> <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>command<span style="color: #339933;">,</span> params<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> paramKey <span style="color: #339933;">=</span> getCacheKey<span style="color: #009900;">&#40;</span>command<span style="color: #339933;">,</span> params<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">var</span> cachedObj<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>paramKey.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            cachedObj <span style="color: #339933;">=</span> cacheStorage<span style="color: #009900;">&#91;</span>paramKey<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$rootScope.<span style="color: #660066;">debug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $log.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>paramKey <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; =&gt; &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>cachedObj <span style="color: #339933;">?</span> <span style="color: #3366CC;">'hit'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> cachedObj<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    put <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>command<span style="color: #339933;">,</span> params<span style="color: #339933;">,</span> deferredObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> paramKey <span style="color: #339933;">=</span> getCacheKey<span style="color: #009900;">&#40;</span>command<span style="color: #339933;">,</span> params<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>paramKey.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            cacheStorage<span style="color: #009900;">&#91;</span>paramKey<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> deferredObj<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Explanation: If you know Angular, you probably knew about $log and $rootScope. If not, just assume that these are variables injected by Angular API. The cache tries to form a key and save the object in the cache map for the key. We sort the params before forming the key because we do not want to duplicate the same object just because user gave params in a different order. </p>
<h3>Data Source Client</h3>
<p>Now that, our cache is ready, we need to implement a client which uses this cache and can be a interface to all the modules on the page. The requirements of the client is, to provide a generic interface to all the calls to a particular website because we wrote the cache store for a single domain. If multiple domains are involved, it is only a matter of time we edit the cache storage to modify key that includes domain name or the complete url.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> url_defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> key<span style="color: #339933;">:</span> $rootScope.<span style="color: #660066;">key</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> doDSCmd<span style="color: #009900;">&#40;</span> command<span style="color: #339933;">,</span> params <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>params<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> params<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">var</span> cachedObj <span style="color: #339933;">=</span> dsCacheStore.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span>command<span style="color: #339933;">,</span> params<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>cachedObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">'deferredObj'</span> <span style="color: #339933;">:</span> cachedObj<span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'fromCache'</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> $rootScope.<span style="color: #660066;">server</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;/&quot;</span> <span style="color: #339933;">+</span> command <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.json&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> deferredObj <span style="color: #339933;">=</span> $http.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> params<span style="color: #339933;">:</span> angular.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> url_defaults<span style="color: #339933;">,</span> params <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    deferredObj.<span style="color: #660066;">success</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> status<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$rootScope.<span style="color: #660066;">debug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $log.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>command <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;: &quot;</span> <span style="color: #339933;">+</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> status<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $log.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;error $http failed with &quot;</span> <span style="color: #339933;">+</span> status <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; for &quot;</span> <span style="color: #339933;">+</span> url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    dsCacheStore.<span style="color: #660066;">put</span><span style="color: #009900;">&#40;</span>command<span style="color: #339933;">,</span> params<span style="color: #339933;">,</span> deferredObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">'deferredObj'</span> <span style="color: #339933;">:</span> deferredObj<span style="color: #339933;">,</span>
        <span style="color: #3366CC;">'fromCache'</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    executeCommand <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>command<span style="color: #339933;">,</span> params<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> doDSCmd<span style="color: #009900;">&#40;</span>command<span style="color: #339933;">,</span> params<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Explanation: We are trying to provide a interface with just one public method: executeCommand &#8211; which means executing a JSON call. The client is trying to read from cache and if not found, it creates a promise object by <b>var deferredObj = $http.get(url, { params: angular.extend( {}, url_defaults, params ) } );</b>. Consider this, as a jQuery equivalent of $.ajax(). Now, this promise is stored in the cache. Next time, when we get a hit from the cache, we get the promise object. Since, you always get a promise object, your module can always use .success on the promise every time it executes. If the call is already completed, your .success callback is called immediately else it waits. Here is the trick, since you are not creating a new promise, AJAX call is NOT made. Instead, it works on the existing promise object and gets the response from the promise &#8211; how many ever times you want.</p>
<h3>Sample Module Usage</h3>
<p>Here is an example of how to call from a module.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> commandOutput <span style="color: #339933;">=</span> dsClient.<span style="color: #660066;">executeCommand</span><span style="color: #009900;">&#40;</span>callObj.<span style="color: #660066;">call</span><span style="color: #339933;">,</span> callObj.<span style="color: #660066;">params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> fromCache <span style="color: #339933;">=</span> commandOutput.<span style="color: #660066;">fromCache</span><span style="color: #339933;">;</span>
commandOutput.<span style="color: #660066;">deferredObj</span>.<span style="color: #660066;">success</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     .....
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>NOTE: The JSON call is used for demo purposes. However, JSONP also works and you just have to change from $http.get to $http.jsonp.</p>
<p>Here is a complete<br />
<h4><a href="http://demo.ksankaran.com/genericds/index.html" target="_blank">demo</a></h4>
<h2>Conclusion</h2>
<p>What did we just do: We learned a bit about jQuery promise, how to implement a cache store that stores jQuery promises for a given url and params, how to implement a client API which makes of cache store and provide a public interface to make AJAX calls and how to write a module that makes use of the client.</p>
<div class="ptl_div"><script>
					//<![CDATA[
					document.write('<g:plusone size="standard" annotation="none" href="http://gdmig-ksankaran.com/wp/2013/06/07/cache-jsonp-calls-client-side/"></g:plusone>');
				        //]]&gt;
					</script><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://gdmig-ksankaran.com/wp/2013/06/07/cache-jsonp-calls-client-side/" data-text="Cache JSON(P) calls client side" data-count="vertical">Tweet</a>
<div class="ptl_fb"><script>
							//<![CDATA[
							document.write('<fb:like href="http://gdmig-ksankaran.com/wp/2013/06/07/cache-jsonp-calls-client-side/" show_faces="true" width="225"  action="like" font="" colorscheme="light"></fb:like>');
							//]]&gt;
							</script></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://gdmig-ksankaran.com/wp/2013/06/07/cache-jsonp-calls-client-side/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Loading Techniques</title>
		<link>http://gdmig-ksankaran.com/wp/2013/06/05/image-loading-techniques/</link>
		<comments>http://gdmig-ksankaran.com/wp/2013/06/05/image-loading-techniques/#comments</comments>
		<pubDate>Wed, 05 Jun 2013 06:14:30 +0000</pubDate>
		<dc:creator><![CDATA[Velu Sankaran]]></dc:creator>
				<category><![CDATA[Browser]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[progressive]]></category>
		<category><![CDATA[progressive JPEG]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[interlaced]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jpeg]]></category>

		<guid isPermaLink="false">http://ksankaran.com/wp/?p=187</guid>
		<description><![CDATA[Introduction This article will discuss on the various image loading techniques in web and functionality of each of them. Mostly, we write img tags and leave it at that and we don&#8217;t care what happens from there. But, it&#8217;ll really comes back to haunt us later when there are plenty of images on the site and web&#8230; <a href="http://gdmig-ksankaran.com/wp/2013/06/05/image-loading-techniques/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This article will discuss on the various image loading techniques in web and functionality of each of them. Mostly, we write img tags and leave it at that and we don&#8217;t care what happens from there. But, it&#8217;ll really comes back to haunt us later when there are plenty of images on the site and web site drags itself.</p>
<h2>Techniques</h2>
<h3>Baseline JPEG rendering</h3>
<p>DEMO: <a href="http://demo.ksankaran.com/progressive/?mode=baseline" target="_blank">link here</a><br />
Baseline JPEGs are the default JPEG files we use. The images are rendered from top to bottom with each layer after another. Please look at the below GIF for understanding the loading process.</p>
<p style="text-align: center;"><img class="thumbnail size-full wp-image-199 aligncenter" alt="baseline-anim" src="http://ksankaran.com/wp/wp-content/uploads/2013/06/baseline-anim.gif" width="450" height="281" /></p>
<p>Now to the details, the image is fetched from the server and as soon as it gets a line, it draws there. As you can see from a test below, it took 7.6 seconds to complete the image rendering and the spinner was spinning till the end &#8211; which means, you are having a bad user experience of just painting few lines of image every second and though the document got ready, the end user is under the impression the document is still loading.</p>
<p style="text-align: center;"><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/06/baselineloading.jpg" target="_blank"><img class="wp-image-188 aligncenter" alt="baselineloading" src="http://ksankaran.com/wp/wp-content/uploads/2013/06/baselineloading.jpg" width="900" height="99" /></a></p>
<h3>Progressive JPEG rendering</h3>
<p>DEMO: <a href="http://demo.ksankaran.com/progressive/?mode=progressive" target="_blank">link here</a><br />
Progressive JPEGs are special kind of JPEGs which has to undergo few processes to create progressive JPEG from normal JPEG. The progressive JPEGs render in a different way &#8211; interlaced. Meaning, all the rows are rendered with first with minimum detail and as download progresses, the quality of image improves.</p>
<p style="text-align: center;"><img class="thumbnail size-full wp-image-202 aligncenter" alt="progressive-anim" src="http://ksankaran.com/wp/wp-content/uploads/2013/06/progressive-anim.gif" width="450" height="281" /></p>
<p>As you can see, the entire picture is visible right from the time page is loaded except the data is fetched continuously to enhance the picture.</p>
<p>Both, baseline and progressive will delay the load event.</p>
<h3>Creating Progressive Images (Mac OS X)</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">jpegtran <span style="color: #660033;">-copy</span> none <span style="color: #660033;">-optimize</span> hawkeye-original.jpg <span style="color: #000000; font-weight: bold;">&gt;</span> hawkeye-opt.jpg
jpegtran <span style="color: #660033;">-copy</span> none <span style="color: #660033;">-progressive</span> hawkeye-opt.jpg <span style="color: #000000; font-weight: bold;">&gt;</span> hawkeye-progressive.jpg</pre></td></tr></table></div>

<p>Progressive takes almost the same computation memory and won&#8217;t bring the browser down. However, JPEGs are a compressed format and progressive is much more optimized in it but this cannot be said for other formats. PNG renders better than JPEG in progressive. </p>
<h3>Simulate Progressive with JS</h3>
<p>DEMO: <a href="http://demo.ksankaran.com/progressive/?mode=lazyload" target="_blank">link here</a><br />
This is an idea to load the cheapest version of the image first and in a separate thread update with a HD version.</p>
<p style="text-align: center;"><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/06/lazy1.png" target="_blank"><img class="size-medium wp-image-206 aligncenter" alt="lazy1" src="http://ksankaran.com/wp/wp-content/uploads/2013/06/lazy1-300x187.png" width="300" height="187" /></a></p>
<p>&nbsp;</p>
<p style="text-align: center;"><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/06/lazy2.png" target="_blank"><img class="size-medium wp-image-205 aligncenter" alt="lazy2" src="http://ksankaran.com/wp/wp-content/uploads/2013/06/lazy2-300x187.png" width="300" height="187" /></a></p>
<p>&nbsp;</p>
<p>The image attribute should have low quality src and a reference to high quality src for us to load later.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
     <span style="color: #339933;">&lt;</span>img refresh<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://ksankaran.com/img/hawkeye-original.jpg?time=&lt;?php echo time(); ?&gt;&quot;</span> src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://ksankaran.com/img/hawkeye-small.jpg?time=&lt;?php echo time(); ?&gt;&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100%&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>On document ready, create a timeout thread to update the src:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
           $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img[refresh]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>idx<span style="color: #339933;">,</span> element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                 <span style="color: #000066; font-weight: bold;">var</span> refreshURL <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'refresh'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 $<span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span> refreshURL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>With this JS simulation, we get a better user experience and document load is fired right after the low quality version is loaded. The only disadvantage is, increased network traffic with small and HD version loading for every image. However, if it is used for only HD images on the page, this is an great approach.
<div class="ptl_div"><script>
					//<![CDATA[
					document.write('<g:plusone size="standard" annotation="none" href="http://gdmig-ksankaran.com/wp/2013/06/05/image-loading-techniques/"></g:plusone>');
				        //]]&gt;
					</script><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://gdmig-ksankaran.com/wp/2013/06/05/image-loading-techniques/" data-text="Image Loading Techniques" data-count="vertical">Tweet</a>
<div class="ptl_fb"><script>
							//<![CDATA[
							document.write('<fb:like href="http://gdmig-ksankaran.com/wp/2013/06/05/image-loading-techniques/" show_faces="true" width="225"  action="like" font="" colorscheme="light"></fb:like>');
							//]]&gt;
							</script></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://gdmig-ksankaran.com/wp/2013/06/05/image-loading-techniques/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to create a OG Action, Submit and get Approval</title>
		<link>http://gdmig-ksankaran.com/wp/2013/05/20/create-og-action-submit-approval/</link>
		<comments>http://gdmig-ksankaran.com/wp/2013/05/20/create-og-action-submit-approval/#comments</comments>
		<pubDate>Mon, 20 May 2013 18:59:49 +0000</pubDate>
		<dc:creator><![CDATA[Velu Sankaran]]></dc:creator>
				<category><![CDATA[Facebook API]]></category>
		<category><![CDATA[Facebook Open Graph]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://ksankaran.com/wp/?p=153</guid>
		<description><![CDATA[Introduction This document is meant to explain how to create a Open Graph action in Facebook, submit your first OG and then get your approval from Facebook. Social Graph In order to understand open graph, we shall first understand social graph. As name suggests, it&#8217;s a graph connecting users by means of social relationship. Eg: User1&#8230; <a href="http://gdmig-ksankaran.com/wp/2013/05/20/create-og-action-submit-approval/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2 id="HowtocreateaOGAction,SubmitandgetApproval-Introduction">Introduction</h2>
<p>This document is meant to explain how to create a Open Graph action in Facebook, submit your first OG and then get your approval from Facebook.</p>
<h2 id="FacebookOpenGraphProtocol-SocialGraph">Social Graph</h2>
<p>In order to understand open graph, we shall first understand social graph. As name suggests, it&#8217;s a graph connecting users by means of social relationship. Eg: User1 is a friend of User2, User1&#8242;s spouse is User4 and so on. It&#8217;s not just a user relationship with another user but with other objects as well. Eg: User1 <strong>watched</strong> a <strong>video</strong> on youtube. In the above social graph action, &#8220;video&#8221; is the social graph object and &#8220;watch&#8221; is the social graph action or relationship of User1 with the social graph object, video. A pictorial snippet of a large open graph is given below (<a href="http://en.wikipedia.org/wiki/Social_graph" rel="nofollow">source</a>):</p>
<p>The first image shows how a social graph looks like from way above and the second is a zoomed in version.</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/13.png" target="_blank"><img class="alignnone size-full wp-image-182" alt="1" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/13.png" width="250" height="294" /></a> <a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/21.png" target="_blank"><img class="alignnone size-medium wp-image-183" alt="2" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/21-300x225.png" width="300" height="225" /></a></p>
<p>So, basically, social graph tries to define the real world social relationships of everyone with every other objects (other people, items, etc). Simplifying a bit more, <strong>social graph defines the relationship of social objects</strong>. A social object can be a user, item or anything in the world.</p>
<h2 id="FacebookOpenGraphProtocol-FacebookOpenGraphProtocol">Facebook Open Graph Protocol</h2>
<p>Its easy &#8211; Open Graph Protocol allows a developer to define and integrate social graph on their websites. The relationship will however be tracked at Facebook but the medium to update relationship is provided by developer website. For example, a website can have a like button and clicking on the button updates your Facebook account &#8211; you liked their website. If you reconsider the above line one more time, we will understand that data is transferred to Facebook from the website in order for Facebook to understand the social interaction. Facebook is not human and cannot understand what open graph action just took place. Simply put, Facebook needs to understand that, the social object (user) has performed a social action (like) on another social object (website). The question is, how do we transfer the open graph data?</p>
<h3 id="FacebookOpenGraphProtocol-OGmetadata">OG metadata</h3>
<p>The answer is, using metadata tags. A sample is given below:</p>
<pre language="html">
&lt;meta property="fb:app_id" content="APP_ID" /&gt;
&lt;meta property="og:type"   content="velublog:process" /&gt;
&lt;meta property="og:url"    content="http://demo.ksankaran.com/fbogdemo/createtestog.html" /&gt;
&lt;meta property="og:title"  content="Sample Custom OG Creation Demo" /&gt;
&lt;meta property="og:image"  content="http://ksankaran.com/img/MyWebsiteLogo-75x75.jpg" /&gt;
</pre>
<p>Let&#8217;s take a deeper look at the above lines. All OG tags start with &#8220;og:&#8221;. The title defines the title of the object being interacted. The type defines the type of object being shared and based on type, it may/may not be required to provide additional attributes. For example, a Recipe object needs data like name of the recipe, time taken to prepare, etc. For complete list of tags with their meaning: please read <a href="https://developers.facebook.com/docs/opengraph/creating-object-types/#properties" rel="nofollow">here</a>.</p>
<h2 id="HowtocreateaOGAction,SubmitandgetApproval-StepbyStepProcess">Step by Step Process</h2>
<p>DEMO: <a href="http://demo.ksankaran.com/fbogdemo/createtestog.html" target="_blank">Click here</a> for posting a custom OG from my blog.</p>
<p>Before we go to the process, it is ideal and recommended that the page on which the OG resides is in descent shape. The HTML shown below is a quick way to submit your OG action but never enough for review submission.</p>
<p>First, go to developers.facebook.com and choose Apps at the top nav. Now, select the app on which you have to create the action. For this document, I will be using my blog&#8217;s app that I own. When you get to the app, click on &#8220;Edit App&#8221; and get to Open Graph. Click on &#8220;Create a Story&#8221;. Now for this document purpose, I have created an action &#8220;Study&#8221; and object &#8220;Process&#8221;. After you submitted that form, you should see the detail as listed below.</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/1.png" target="_blank"><img class="alignnone size-medium wp-image-154" alt="1" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/1-300x196.png" width="300" height="196" /></a></p>
<p>Navigate to Open Graph (left nav) -&gt; Types and select the newly created object &#8220;Process&#8221; and at the bottom, choose the right set of permissions you need.</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/2.png" target="_blank"><img class="alignnone size-medium wp-image-155" alt="2" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/2-300x135.png" width="300" height="135" /></a></p>
<p>Now, go on to &#8220;Review Status&#8221; on the left nav and you will see that your object submission is now grayed out.</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/3.png" target="_blank"><img class="alignnone size-medium wp-image-156" alt="3" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/3-300x114.png" width="300" height="114" /></a></p>
<p>This means, you need to create a sample page and submit the OG to Facebook to create your first OG action before your submit this action to public.</p>
<p>In order to create the action, you need to create a sample page and then execute code from there. Go to open graph -&gt; stories and click on &#8220;Get Code&#8221; on your new object.</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/4.png" target="_blank"><img class="alignnone size-medium wp-image-157" alt="4" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/4-300x156.png" width="300" height="156" /></a></p>
<p>Select &#8220;Code for Object&#8221;</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/5.png" target="_blank"><img class="alignnone size-medium wp-image-158" alt="5" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/5-300x161.png" width="300" height="161" /></a></p>
<p>Now copy the code and paste into HTML. Also, add your own code to it to allow it to submit the OG.</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/6.png" target="_blank"><img class="alignnone size-medium wp-image-159" alt="6" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/6-300x275.png" width="300" height="275" /></a></p>
<p>Not the best code but it will work. For you convenience, I&#8217;m sharing this code (<a href="http://demo.ksankaran.com/fbogdemo/createtestog.html" target="_blank">link here</a>).</p>
<p>When you load the page, it should look like this:</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/7.png" target="_blank"><img class="alignnone size-medium wp-image-160" alt="7" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/7-300x111.png" width="300" height="111" /></a></p>
<p>Click on the button and make sure you login with the ID who created the action. The output should look like below:</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/8.png" target="_blank"><img class="alignnone size-full wp-image-161" alt="8" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/8.png" width="300" height="121" /></a></p>
<p>Copy the id that comes up and append it to facebook.com:</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/9.png" target="_blank"><img class="alignnone size-full wp-image-162" alt="9" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/9.png" width="260" height="36" /></a></p>
<p>It should take you to your newly created activity:</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/10.png" target="_blank"><img class="alignnone size-medium wp-image-163" alt="10" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/10-300x156.png" width="300" height="156" /></a></p>
<p>Now, you should see the submit button enabled in your APP:</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/11.png" target="_blank"><img class="alignnone size-medium wp-image-164" alt="11" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/11-300x137.png" width="300" height="137" /></a></p>
<p>Click on submit and it will pull up this form like below. BUT, I would strong suggest our production page is ready before the submission process. Or at least a quality working page.</p>
<p><a class="thumbnail" href="http://ksankaran.com/wp/wp-content/uploads/2013/05/12.png" target="_blank"><img class="alignnone size-medium wp-image-165" alt="12" src="http://ksankaran.com/wp/wp-content/uploads/2013/05/12-300x255.png" width="300" height="255" /></a></p>
<p>Filling this form is straightforward and needs to be done by the product owner.</p>
<p>&nbsp;
<div class="ptl_div"><script>
					//<![CDATA[
					document.write('<g:plusone size="standard" annotation="none" href="http://gdmig-ksankaran.com/wp/2013/05/20/create-og-action-submit-approval/"></g:plusone>');
				        //]]&gt;
					</script><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://gdmig-ksankaran.com/wp/2013/05/20/create-og-action-submit-approval/" data-text="How to create a OG Action, Submit and get Approval" data-count="vertical">Tweet</a>
<div class="ptl_fb"><script>
							//<![CDATA[
							document.write('<fb:like href="http://gdmig-ksankaran.com/wp/2013/05/20/create-og-action-submit-approval/" show_faces="true" width="225"  action="like" font="" colorscheme="light"></fb:like>');
							//]]&gt;
							</script></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://gdmig-ksankaran.com/wp/2013/05/20/create-og-action-submit-approval/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Modify YO Scaffolding Paths</title>
		<link>http://gdmig-ksankaran.com/wp/2013/04/29/modify-yo-scaffolding-paths/</link>
		<comments>http://gdmig-ksankaran.com/wp/2013/04/29/modify-yo-scaffolding-paths/#comments</comments>
		<pubDate>Mon, 29 Apr 2013 20:10:53 +0000</pubDate>
		<dc:creator><![CDATA[Velu Sankaran]]></dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Yeoman]]></category>

		<guid isPermaLink="false">http://ksankaran.com/wp/?p=115</guid>
		<description><![CDATA[Introduction Yeoman is a very good framework for doing three things 1) scaffolding 2) dependency management and 3) build, preview &#38; test. Yo is the component which does the code scaffolding and we are going to look a bit deeper on how to tune the generators to suit our need and structure. Installing Yeoman and&#8230; <a href="http://gdmig-ksankaran.com/wp/2013/04/29/modify-yo-scaffolding-paths/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Yeoman is a very good framework for doing three things 1) scaffolding 2) dependency management and 3) build, preview &amp; test. Yo is the component which does the code scaffolding and we are going to look a bit deeper on how to tune the generators to suit our need and structure.</p>
<h2>Installing Yeoman and Git Protocol Problems</h2>
<p>When dependencies are pulled via github, the protocol followed is git:// and is blocked by firewall at few places. In order for that to resolve, you need to make sure git uses http(s) instead of git. So, please execute the following line to make sure GIT follows HTTP protocol.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git config</span> <span style="color: #660033;">--global</span> url.<span style="color: #ff0000;">&quot;https://&quot;</span>.insteadOf git:<span style="color: #000000; font-weight: bold;">//</span></pre></td></tr></table></div>

<p>After the above steps you should not have problems when using npm install or bower install.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">npm <span style="color: #c20cb9; font-weight: bold;">install</span> generator-angular generator-karma  <span style="color: #666666; font-style: italic;"># install generators</span>
yo angular                     <span style="color: #666666; font-style: italic;"># scaffold out a AngularJS project</span>
npm <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> bower <span style="color: #c20cb9; font-weight: bold;">install</span>   <span style="color: #666666; font-style: italic;"># install default dependencies</span>
bower <span style="color: #c20cb9; font-weight: bold;">install</span> angular-ui       <span style="color: #666666; font-style: italic;"># install a dependency for your project from Bower</span>
grunt <span style="color: #7a0874; font-weight: bold;">test</span>                     <span style="color: #666666; font-style: italic;"># test your app</span>
grunt server                   <span style="color: #666666; font-style: italic;"># preview your app</span>
grunt                          <span style="color: #666666; font-style: italic;"># build the application for deployment</span></pre></td></tr></table></div>

<h2>Yeoman Generators</h2>
<p>If you take a look at line 1 on the above snippet, if installs angular generator. So, lets take a look at how to scaffold out a AngularJS directive:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">vsankaran-ml2:yeoman3 ksankaran$ yo angular:directive thing1
   create app<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>directives<span style="color: #000000; font-weight: bold;">/</span>thing1.js
   create test<span style="color: #000000; font-weight: bold;">/</span>spec<span style="color: #000000; font-weight: bold;">/</span>directives<span style="color: #000000; font-weight: bold;">/</span>thing1.js</pre></td></tr></table></div>

<p>When you install angular generator using &#8220;npm install angular-generator&#8221;, it may not installed in the actual yo library for users in Mountain Lion (10.8) or greater. So, if you execute the command &#8220;yo angular:directive thing1&#8243;, you might get an error saying that generator is not registered. To fix, please execute the following commands.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>node_modules<span style="color: #000000; font-weight: bold;">/</span>yo
npm <span style="color: #c20cb9; font-weight: bold;">install</span> generator-angular</pre></td></tr></table></div>

<p>As you can see from above, it creates two things: 1) the directive itself and 2) the test case for the directive. This is where Yeoman&#8217;s strength is.</p>
<p><a href="http://ksankaran.com/wp/wp-content/uploads/2013/04/yo1.png" target="_blank"><img class="alignnone size-medium wp-image-120" alt="yo1" src="http://ksankaran.com/wp/wp-content/uploads/2013/04/yo1-300x203.png" width="300" height="203" /></a></p>
<h2>Modify Generator Behavior</h2>
<p>Ok, now that we have scaffolded out a directive, how do I make sure that we scaffold out in a proper structure that we wanted to? The answer is not simple and we have to modify generator&#8217;s source to achieve that. I know, this is extremely dangerous, but the path and other stuffs is &#8220;hardcoded&#8221; in the generator code. Before we dig deep into the generator code, please understand that the generator code is located at: /usr/local/lib/node_modules/yo/node_modules/generator-angular.</p>
<p>Now, lets take a look at directive generator code snippet in index.js inside directives (which is inside the generator code) folder:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Generator.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">createDirectiveFiles</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> createDirectiveFiles<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">appTemplate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'directive'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'scripts/directives/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">testTemplate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'spec/directive'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'directives/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addScriptToIndex</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'directives/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The line this.appTemplate generates the directive file inside script/directives folder. Now we wanted to pass in an extra argument to accomodate module parameter, so that the file gets dropped at scripts/module_name/directives folders (this is just an example and not a final structure). You need to modify the code above like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Generator.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">createDirectiveFiles</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> createDirectiveFiles<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">args</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">args</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> module <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">args</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">appTemplate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'directive'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'scripts/'</span><span style="color: #339933;">+</span>module<span style="color: #339933;">+</span><span style="color: #3366CC;">'/directives/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">appTemplate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'directive'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'scripts/directives/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">testTemplate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'spec/directive'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'directives/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addScriptToIndex</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'directives/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, on executing the command</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">vsankaran<span style="color: #339933;">-</span>ml2<span style="color: #339933;">:</span>yeoman3 ksankaran$ yo angular<span style="color: #339933;">:</span>directive thing2 module1
   create app<span style="color: #339933;">/</span>scripts<span style="color: #339933;">/</span>module1<span style="color: #339933;">/</span>directives<span style="color: #339933;">/</span>thing2.<span style="color: #660066;">js</span>
   create test<span style="color: #339933;">/</span>spec<span style="color: #339933;">/</span>directives<span style="color: #339933;">/</span>thing2.<span style="color: #660066;">js</span></pre></td></tr></table></div>

<p>it creates a module1 folder and places the directive file inside it. If you want to change the test script file location, you can change the this.testTemplate command.</p>
<p><a href="http://ksankaran.com/wp/wp-content/uploads/2013/04/yo2.png" target="_blank"><img class="alignnone size-medium wp-image-131" alt="yo2" src="http://ksankaran.com/wp/wp-content/uploads/2013/04/yo2-300x230.png" width="300" height="230" /></a></p>
<p>That&#8217;s it folks. This is how you modify yo generator code for angular.
<div class="ptl_div"><script>
					//<![CDATA[
					document.write('<g:plusone size="standard" annotation="none" href="http://gdmig-ksankaran.com/wp/2013/04/29/modify-yo-scaffolding-paths/"></g:plusone>');
				        //]]&gt;
					</script><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://gdmig-ksankaran.com/wp/2013/04/29/modify-yo-scaffolding-paths/" data-text="Modify YO Scaffolding Paths" data-count="vertical">Tweet</a>
<div class="ptl_fb"><script>
							//<![CDATA[
							document.write('<fb:like href="http://gdmig-ksankaran.com/wp/2013/04/29/modify-yo-scaffolding-paths/" show_faces="true" width="225"  action="like" font="" colorscheme="light"></fb:like>');
							//]]&gt;
							</script></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://gdmig-ksankaran.com/wp/2013/04/29/modify-yo-scaffolding-paths/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Browser Stats with MongoLab &amp; Google Charts</title>
		<link>http://gdmig-ksankaran.com/wp/2013/04/25/browser-stats-with-mongolab-google-charts/</link>
		<comments>http://gdmig-ksankaran.com/wp/2013/04/25/browser-stats-with-mongolab-google-charts/#comments</comments>
		<pubDate>Thu, 25 Apr 2013 20:05:55 +0000</pubDate>
		<dc:creator><![CDATA[Velu Sankaran]]></dc:creator>
				<category><![CDATA[Browser]]></category>
		<category><![CDATA[Google Charts]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[mongolab]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[stats]]></category>

		<guid isPermaLink="false">http://ksankaran.com/wp/?p=95</guid>
		<description><![CDATA[Introduction This tutorial will help you learn many things &#8211; how to use MongoLab API for CRUD operations, how to get browser info and version and how to use google charts. What we are going to achieve is, to create a real-time page which keeps updating the hits with browser info and also renders the&#8230; <a href="http://gdmig-ksankaran.com/wp/2013/04/25/browser-stats-with-mongolab-google-charts/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This tutorial will help you learn many things &#8211; how to use MongoLab API for CRUD operations, how to get browser info and version and how to use google charts. What we are going to achieve is, to create a real-time page which keeps updating the hits with browser info and also renders the page with the stats.</p>
<p><img class="alignnone size-medium wp-image-96" alt="googlechart" src="http://ksankaran.com/wp/wp-content/uploads/2013/04/googlechart-300x187.jpg" width="300" height="187" /></p>
<h2>Pre-requisite</h2>
<p>You should at least know what MongoDB is and the flexibility it offers. You can read it right <a href="http://www.mongodb.org/">here</a>. MongoLab is the cloud API provider for MongoDB. It also provides a REST interface for the same. Opening a account is free and so I would advise you to get one in case if you wanted to try MongoLab in your small projects. Do NOT try and use the key I used in my JavaScript. It&#8217;s not going to cost me anything because its a free account but again, I would have to manually login and clear those items if you use my ID for your demo apps :).</p>
<h2>Storing browser info</h2>
<p><span style="line-height: 1.714285714; font-size: 1rem;">The idea is to figure out the browser name and version and if it is already in DB, just increment the hit number else insert a new row.</span></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
    Util method to get browserName and browserVersion from navigator. Source: stackoverflow.com.
 */</span>
navigator.<span style="color: #660066;">sayswho</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> N <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">appName</span><span style="color: #339933;">,</span>
        ua <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">userAgent</span><span style="color: #339933;">,</span>
        tem<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> M <span style="color: #339933;">=</span> ua.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>M <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>tem <span style="color: #339933;">=</span> ua.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/version\/([\.\d]+)/i</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> M<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> tem<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    M <span style="color: #339933;">=</span> M <span style="color: #339933;">?</span> <span style="color: #009900;">&#91;</span>M<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> M<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>N<span style="color: #339933;">,</span> navigator.<span style="color: #660066;">appVersion</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'-?'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> M<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>navigator.<span style="color: #660066;">sayswho</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    navigator.<span style="color: #660066;">cBrowserName</span> <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">sayswho</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    navigator.<span style="color: #660066;">cBrowserVersion</span> <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">sayswho</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now you do have browser name and version after the above steps. The task is to figure out whether the row is already present in DB or not. Querying the DB is easy with JavaScript but it is a shame that you can&#8217;t do that for commercial website because people will use the key to query your DB directly.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;https://api.mongolab.com/api/1/databases/webdev/collections/hits?apiKey=&lt;YOUR_KEY&gt;&amp;q=&quot;</span> <span style="color: #339933;">+</span> queryStr<span style="color: #339933;">,</span>
        type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span>
        contentType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;application/json&quot;</span><span style="color: #339933;">,</span>
        async<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        timeout<span style="color: #339933;">:</span> <span style="color: #CC0000;">5000</span><span style="color: #339933;">,</span>
        success<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            ..
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        error<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>xhr<span style="color: #339933;">,</span> status<span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The insert and update code is pretty straightforward.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> updateDB<span style="color: #009900;">&#40;</span>browser<span style="color: #339933;">,</span> version<span style="color: #339933;">,</span> hits<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> queryObj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;browser&quot;</span><span style="color: #339933;">:</span> browser<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;version&quot;</span><span style="color: #339933;">:</span> version<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> queryStr <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>queryObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// API Key needs to be changed to yours. This key is, however, a free limited space key and you can get one for yourself.</span>
    <span style="color: #006600; font-style: italic;">// https://mongolab.com/welcome/ : Sign up and get 500MB free.</span>
    $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'https://api.mongolab.com/api/1/databases/webdev/collections/hits?apiKey=&lt;YOUR_KEY&gt;&amp;q='</span> <span style="color: #339933;">+</span> queryStr<span style="color: #339933;">,</span>
        data<span style="color: #339933;">:</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;$set&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;hits&quot;</span> <span style="color: #339933;">:</span> hits <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;PUT&quot;</span><span style="color: #339933;">,</span>
        contentType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;application/json&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/*
    Insert the new browser info here.
 */</span>
<span style="color: #000066; font-weight: bold;">function</span> insertToDB<span style="color: #009900;">&#40;</span>browser<span style="color: #339933;">,</span> version<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// API Key needs to be changed to yours. This key is, however, a free limited space key and you can get one for yourself.</span>
    <span style="color: #006600; font-style: italic;">// https://mongolab.com/welcome/ : Sign up and get 500MB free.</span>
    $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;https://api.mongolab.com/api/1/databases/webdev/collections/hits?apiKey=&lt;YOUR_KEY&gt;&quot;</span><span style="color: #339933;">,</span>
        data<span style="color: #339933;">:</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;browser&quot;</span> <span style="color: #339933;">:</span> browser<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;version&quot;</span> <span style="color: #339933;">:</span> version<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;hits&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
        contentType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;application/json&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The posting part is now over.</p>
<h2>Get browser stats</h2>
<p>Let&#8217;s query the DB with browser name as sort order.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;https://api.mongolab.com/api/1/databases/webdev/collections/hits?apiKey=&lt;YOUR_KEY&gt;&amp;s=&quot;</span><span style="color: #339933;">+</span>queryStr<span style="color: #339933;">,</span>
    type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span>
    contentType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;application/json&quot;</span><span style="color: #339933;">,</span>
    async<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    timeout<span style="color: #339933;">:</span> <span style="color: #CC0000;">5000</span><span style="color: #339933;">,</span>
    success<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ..
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    error<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>xhr<span style="color: #339933;">,</span> status<span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Google charts is a fantastic API to display charts. It is the easiest way and also the best I&#8217;ve seen.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">google.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;visualization&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>packages<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;corechart&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
google.<span style="color: #660066;">setOnLoadCallback</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>callbackmethod_to_query_DB<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
.......
<span style="color: #006600; font-style: italic;">// after query</span>
<span style="color: #000066; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> google.<span style="color: #660066;">visualization</span>.<span style="color: #660066;">arrayToDataTable</span><span style="color: #009900;">&#40;</span>browserData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Browser Stats'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> chart <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> google.<span style="color: #660066;">visualization</span>.<span style="color: #660066;">PieChart</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'chart_div'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
chart.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s it folks! Take a look at <a href="http://demo.ksankaran.com/mongo/">demo here</a>.
<div class="ptl_div"><script>
					//<![CDATA[
					document.write('<g:plusone size="standard" annotation="none" href="http://gdmig-ksankaran.com/wp/2013/04/25/browser-stats-with-mongolab-google-charts/"></g:plusone>');
				        //]]&gt;
					</script><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://gdmig-ksankaran.com/wp/2013/04/25/browser-stats-with-mongolab-google-charts/" data-text="Browser Stats with MongoLab &#038; Google Charts" data-count="vertical">Tweet</a>
<div class="ptl_fb"><script>
							//<![CDATA[
							document.write('<fb:like href="http://gdmig-ksankaran.com/wp/2013/04/25/browser-stats-with-mongolab-google-charts/" show_faces="true" width="225"  action="like" font="" colorscheme="light"></fb:like>');
							//]]&gt;
							</script></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://gdmig-ksankaran.com/wp/2013/04/25/browser-stats-with-mongolab-google-charts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>1-Player Tic Tac Toe in AngularJS</title>
		<link>http://gdmig-ksankaran.com/wp/2013/04/16/tic-tac-toe-in-angularjs/</link>
		<comments>http://gdmig-ksankaran.com/wp/2013/04/16/tic-tac-toe-in-angularjs/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 16:42:58 +0000</pubDate>
		<dc:creator><![CDATA[Velu Sankaran]]></dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[angularJS]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tictactoe]]></category>

		<guid isPermaLink="false">http://mystupidposts.wordpress.com/?p=63</guid>
		<description><![CDATA[Introduction It&#8217;s been sometime I&#8217;ve been here. So, I will just get started off. Today, I&#8217;m going to write about how to develop tic-tac-toe using AngularJS. The demo is more to understand the possibilities and simplicity of the framework. What&#8217;s needed in the UI 9 boxes arranged in a 3&#215;3 fashion like below: Each of&#8230; <a href="http://gdmig-ksankaran.com/wp/2013/04/16/tic-tac-toe-in-angularjs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>It&#8217;s been sometime I&#8217;ve been here. So, I will just get started off. Today, I&#8217;m going to write about how to develop tic-tac-toe using AngularJS. The demo is more to understand the possibilities and simplicity of the framework.</p>
<h2>What&#8217;s needed in the UI</h2>
<p>9 boxes arranged in a 3&#215;3 fashion like below:<br />
<img class="size-full wp-image-65 aligncenter" alt="square-design" src="http://ksankaran.com/wp/wp-content/uploads/2013/04/square-design.jpg" width="516" height="510" /></p>
<p>Each of these boxes are divs and each div is binded to the data behind as usual</p>
<p>Template:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div ng<span style="color: #339933;">-</span>repeat<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;row in rows&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{{column.id}}&quot;</span> ng<span style="color: #339933;">-</span>repeat<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;column in row&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>div ng<span style="color: #339933;">-</span>click<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;markUserClick(column)&quot;</span><span style="color: #339933;">&gt;</span> <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Data:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$scope.<span style="color: #660066;">rows</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'id'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'A11'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'letter'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'id'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'A12'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'letter'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'id'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'A13'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'letter'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'id'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'B11'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'letter'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'id'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'B12'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'letter'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'id'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'B13'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'letter'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'id'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'C11'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'letter'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'id'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'C12'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'letter'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'id'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'C13'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'letter'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, the logic is to make the JS think engine. It&#8217;s easy when you follow the strategy mentioned in <a href="http://en.wikipedia.org/wiki/Tic-tac-toe#Strategy">wiki</a>.</p>
<p>AI Algorithm:</p>
<ol>
<li>Win: If the AI has two in a row, it will place a third to get three in a row.</li>
<li>Block: If the [opponent] has two in a row, the AI will play the third to block the opponent.</li>
<li>Fork: Creation of an opportunity where the AI has two threats to win (two non-blocked lines of 2).</li>
<li>Blocking an opponent&#8217;s fork:
<ol>
<li>The AI will create two in a row to force the opponent into defending, as long as it doesn&#8217;t result in them creating a fork. For example, if &#8220;X&#8221; has a corner, &#8220;O&#8221; has the center, and &#8220;X&#8221; has the opposite corner as well, &#8220;O&#8221; must not play a corner in order to win. (Playing a corner in this scenario creates a fork for &#8220;X&#8221; to win.)</li>
<li>If there is a configuration where the opponent can fork, the player should block that fork.</li>
</ol>
</li>
<li>Center: AI marks the center. (If it is the first move of the game, playing on a corner gives &#8220;O&#8221; more opportunities to make a mistake and may therefore be the better choice; however, it makes no difference between perfect players.)</li>
<li>Opposite corner: If the opponent is in the corner, the AI plays the opposite corner.</li>
<li>Empty corner: The AI plays in a corner square.</li>
<li>Empty side: The AI plays in a middle square on any of the 4 sides.</li>
</ol>
<h1>For TicTacToe DEMO : <a target="_blank" href="http://demo.ksankaran.com/tictactoe/" target="_blank">Click Here</a>.</h1>
<p>That&#8217;s it folks. Enjoy playing.
<div class="ptl_div"><script>
					//<![CDATA[
					document.write('<g:plusone size="standard" annotation="none" href="http://gdmig-ksankaran.com/wp/2013/04/16/tic-tac-toe-in-angularjs/"></g:plusone>');
				        //]]&gt;
					</script><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://gdmig-ksankaran.com/wp/2013/04/16/tic-tac-toe-in-angularjs/" data-text="1-Player Tic Tac Toe in AngularJS" data-count="vertical">Tweet</a>
<div class="ptl_fb"><script>
							//<![CDATA[
							document.write('<fb:like href="http://gdmig-ksankaran.com/wp/2013/04/16/tic-tac-toe-in-angularjs/" show_faces="true" width="225"  action="like" font="" colorscheme="light"></fb:like>');
							//]]&gt;
							</script></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://gdmig-ksankaran.com/wp/2013/04/16/tic-tac-toe-in-angularjs/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Get Close Friends and Family members &#8211; Facebook API</title>
		<link>http://gdmig-ksankaran.com/wp/2012/10/12/get-close-friends-and-family-members-facebook-api/</link>
		<comments>http://gdmig-ksankaran.com/wp/2012/10/12/get-close-friends-and-family-members-facebook-api/#comments</comments>
		<pubDate>Fri, 12 Oct 2012 17:58:03 +0000</pubDate>
		<dc:creator><![CDATA[Velu Sankaran]]></dc:creator>
				<category><![CDATA[Facebook API]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[technology]]></category>

		<guid isPermaLink="false">http://mystupidposts.wordpress.com/?p=44</guid>
		<description><![CDATA[Hey guys, Recently, I have been working on integration of weather.com with Facebook via Facebook API calls. One of the challenges I faced is how easily can we get close friends and families without increasing number of calls. The Graph API is so vast and enables us to do it at one shot. It is&#8230; <a href="http://gdmig-ksankaran.com/wp/2012/10/12/get-close-friends-and-family-members-facebook-api/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Hey guys,</p>
<p>Recently, I have been working on integration of weather.com with Facebook via Facebook API calls. One of the challenges I faced is how easily can we get close friends and families without increasing number of calls. The Graph API is so vast and enables us to do it at one shot. It is for sure you can do it by different ways but if you prefer Graph API, this is how you do:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// The following API call gets the list of members from family list and displays in console.</span>
FB.<span style="color: #660066;">api</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/me/friendlists/family?fields=members&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
     console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// and to get close friends</span>
FB.<span style="color: #660066;">api</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/me/friendlists/close_friends?fields=members&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
     console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As you can see from above, family and close_friends are keywords/id by itself and don&#8217;t need the specific group ID. Oh, and don&#8217;t forget &#8211; you need read_friendlists permission for your APP or you get it in your scope during your login.</p>
<p>If you are a looking at getting both members at one shot, there is always FQL.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">FB.<span style="color: #660066;">api</span><span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#123;</span>
        method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'fql.query'</span><span style="color: #339933;">,</span>
        query<span style="color: #339933;">:</span> <span style="color: #3366CC;">'SELECT uid FROM friendlist_member WHERE (flid IN (SELECT flid FROM friendlist WHERE owner=me() and type = &quot;family&quot;) or flid IN (SELECT flid FROM friendlist WHERE owner=me() and type = &quot;close_friends&quot;))'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<div class="ptl_div"><script>
					//<![CDATA[
					document.write('<g:plusone size="standard" annotation="none" href="http://gdmig-ksankaran.com/wp/2012/10/12/get-close-friends-and-family-members-facebook-api/"></g:plusone>');
				        //]]&gt;
					</script><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://gdmig-ksankaran.com/wp/2012/10/12/get-close-friends-and-family-members-facebook-api/" data-text="Get Close Friends and Family members &#8211; Facebook API" data-count="vertical">Tweet</a>
<div class="ptl_fb"><script>
							//<![CDATA[
							document.write('<fb:like href="http://gdmig-ksankaran.com/wp/2012/10/12/get-close-friends-and-family-members-facebook-api/" show_faces="true" width="225"  action="like" font="" colorscheme="light"></fb:like>');
							//]]&gt;
							</script></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://gdmig-ksankaran.com/wp/2012/10/12/get-close-friends-and-family-members-facebook-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
