<?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>Anand Nalya &#187; javascript</title>
	<atom:link href="http://anandnalya.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://anandnalya.com</link>
	<description>blog</description>
	<lastBuildDate>Tue, 01 May 2012 10:25:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Building a Single Page Webapp with jQuery</title>
		<link>http://anandnalya.com/2012/03/19/building-a-single-page-webapp-with-jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-single-page-webapp-with-jquery</link>
		<comments>http://anandnalya.com/2012/03/19/building-a-single-page-webapp-with-jquery/#comments</comments>
		<pubDate>Mon, 19 Mar 2012 10:15:59 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[single page webapp]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=168</guid>
		<description><![CDATA[In a typical single page web app, you load a html page and keep updating it by requesting data from the server either periodically or whenever the user asks for. Such an application is well suited for data driven systems. &#8230; <a href="http://anandnalya.com/2012/03/19/building-a-single-page-webapp-with-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In a typical single page web app, you load a html page and keep updating it by requesting data from the server either periodically or whenever the user asks for. Such an application is well suited for data driven systems. For example, if you already have a REST API, you can easily create a single page web client for it. The best way of understanding what a single page web app is by looking at the web interface of <a href="http://mail.google.com">Gmail</a></p>
<p>So let us define what we want to achieve in the end with our application:</p>
<ul>
<li>We should be able to load different views on the same page.</li>
<li>We should be able to translate user intentions (clicks) into appropriate javascript function calls.</li>
<li>The url shown in the address bar of the browser should represent the state of the application. That is, when the user refreshes the browser, it should have the same content as prior to refresh.</li>
</ul>
<h2>The Root Page</h2>
<p>So let us begin by defining the basic structure of our application. <code>index.html</code> is our application page that will be modified during the lifetime of our application. For the sake of simplicity, let us assume that we will be making changes to <code>#application</code> div only. </p>
<div id="gist-2100959" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;html&gt;</span></div><div class='line' id='LC3'><span class="nt">&lt;head&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src=</span><span class="s">&quot;./scripts/sugar.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src=</span><span class="s">&quot;./scripts/jquery-1.7.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src=</span><span class="s">&quot;./scripts/app.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class='line' id='LC7'><span class="nt">&lt;/head&gt;</span></div><div class='line' id='LC8'><span class="nt">&lt;body&gt;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;doc&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;hd&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Header/Navigation goes here</div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/div&gt;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;mainContent&quot;</span> <span class="na">class=</span><span class="s">&quot;content&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;app&quot;</span> <span class="na">role=</span><span class="s">&quot;application&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This is the content area </div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/div&gt;</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/div&gt;</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;ft&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Footer goes here </div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/div&gt;</span></div><div class='line' id='LC21'><span class="nt">&lt;/body&gt;</span></div><div class='line' id='LC22'><span class="nt">&lt;/html&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2100959/4678e552fb6881e35196d48920aceb387066fd95/index.html" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2100959#file_index.html" style="float:right;margin-right:10px;color:#666">index.html</a>
            <a href="https://gist.github.com/2100959">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Intercepting link clicks</h2>
<p>Now we need to intercept all the click actions on the links and change the url hash accordingly. For handling the url hashes, we are using the <a href="http://benalman.com/projects/jquery-bbq-plugin/">Jquery BBQ plugin</a>. Intercepting the link clicks will help us in preventing the default navigation to other pages. Also, as this is the required behaviour for all the links, we will setup the interceptor at global level. If required you can change the scope of this interception by modifying the jQuery selector where we define the interception.</p>
<div id="gist-2100959" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>	<span class="c1">// convert all a/href to a#href</span></div><div class='line' id='LC4'>	<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;body&quot;</span><span class="p">).</span><span class="nx">delegate</span><span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;click&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC5'>		<span class="kd">var</span> <span class="nx">href</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;href&quot;</span><span class="p">);</span> <span class="c1">// modify the selector here to change the scope of intercpetion</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>		 <span class="c1">// Push this URL &quot;state&quot; onto the history hash.</span></div><div class='line' id='LC8'>		<span class="nx">$</span><span class="p">.</span><span class="nx">bbq</span><span class="p">.</span><span class="nx">pushState</span><span class="p">(</span><span class="nx">href</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>		<span class="c1">// Prevent the default click behavior.</span></div><div class='line' id='LC11'>		<span class="k">return</span> <span class="kc">false</span><span class="p">;</span></div><div class='line' id='LC12'>	<span class="p">});</span></div><div class='line' id='LC13'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2100959/fb58eea5526f9f6ee09ea3dc97d999027852db7d/app_1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2100959#file_app_1.js" style="float:right;margin-right:10px;color:#666">app_1.js</a>
            <a href="https://gist.github.com/2100959">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Defining the Routes</h2>
<p>Next, we need to define the routes that are mapped to url hash. Also, since each hashchange represents an action on part of user, we will also define what function to execute for each hashchange. We do this by listening for <code>haschange</code> event and firing the appropriate js function.</p>
<div id="gist-2100959" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>	<span class="c1">// Bind a callback that executes when document.location.hash changes.</span></div><div class='line' id='LC4'>	<span class="nx">$</span><span class="p">(</span><span class="nb">window</span><span class="p">).</span><span class="nx">bind</span><span class="p">(</span> <span class="s2">&quot;hashchange&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC5'>		<span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">extended</span><span class="p">(</span><span class="nx">$</span><span class="p">.</span><span class="nx">bbq</span><span class="p">.</span><span class="nx">getState</span><span class="p">()).</span><span class="nx">keys</span><span class="p">();</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>		<span class="k">if</span><span class="p">(</span><span class="nx">url</span><span class="p">.</span><span class="nx">length</span><span class="o">==</span><span class="mi">1</span><span class="p">){</span></div><div class='line' id='LC8'>			<span class="nx">url</span> <span class="o">=</span> <span class="nx">url</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span></div><div class='line' id='LC9'>		<span class="p">}</span><span class="k">else</span><span class="p">{</span></div><div class='line' id='LC10'>			<span class="k">return</span><span class="p">;</span></div><div class='line' id='LC11'>		<span class="p">}</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>		<span class="c1">// url action mapping</span></div><div class='line' id='LC14'>		<span class="k">if</span><span class="p">(</span><span class="nx">url</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="sr">/^\/users$/</span><span class="p">)){</span></div><div class='line' id='LC15'>			<span class="nx">showUserList</span><span class="p">();</span></div><div class='line' id='LC16'>		<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="sr">/^\/users\/\d+$/</span><span class="p">)){</span> <span class="c1">// matching /users/1234</span></div><div class='line' id='LC17'>			<span class="nx">showUser</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span></div><div class='line' id='LC18'>		<span class="p">}</span></div><div class='line' id='LC19'>		<span class="c1">// add more routes</span></div><div class='line' id='LC20'>	<span class="p">});</span></div><div class='line' id='LC21'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2100959/792a6310785d4f6eea4f48de190c44a6c95d6e75/app_2.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2100959#file_app_2.js" style="float:right;margin-right:10px;color:#666">app_2.js</a>
            <a href="https://gist.github.com/2100959">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Initialization</h2>
<p>Even though, we have proper routes and actions defined now, our index page is still empty. We now need to initialize the app. This is done by setting the default <code>hash</code>, which will fire the <code>hashchange</code> event when the page loads or in case the user has refreshed the page, just triggering the <code>hashchange</code> event manually.</p>
<div id="gist-2100959" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>	</div><div class='line' id='LC2'>	<span class="k">if</span><span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">hash</span><span class="o">==</span><span class="s1">&#39;&#39;</span><span class="p">){</span></div><div class='line' id='LC3'>		<span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">hash</span><span class="o">=</span><span class="s2">&quot;#/users&quot;</span><span class="p">;</span> <span class="c1">// home page, show the default view (user list)</span></div><div class='line' id='LC4'>	<span class="p">}</span><span class="k">else</span><span class="p">{</span></div><div class='line' id='LC5'>		<span class="nx">$</span><span class="p">(</span><span class="nb">window</span><span class="p">).</span><span class="nx">trigger</span><span class="p">(</span> <span class="s2">&quot;hashchange&quot;</span> <span class="p">);</span> <span class="c1">// user refreshed the browser, fire the appropriate function</span></div><div class='line' id='LC6'>	<span class="p">}</span></div><div class='line' id='LC7'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2100959/afdecf8cf2c80dc79df64d4c8aeca03490bc2315/app_3.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2100959#file_app_3.js" style="float:right;margin-right:10px;color:#666">app_3.js</a>
            <a href="https://gist.github.com/2100959">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Sample Action</h2>
<p>Now that we have the skeleton for our application ready, lets have a look at a sample action. Suppose the default action, is showing a list of users [<code>#/users</code>]. We have mapped this hash to the function <code>showUserList</code>. Now there are several ways of combining the html structure and the json data for userList, but we will be using the simplest of methods. First we will get the html fragment representing the userList using the  <code>$.load</code> function, and then we will populate it with actual data that we get from the REST api.
<div id="gist-2100959" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="nx">showUserList</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC2'>	<span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#app&#39;</span><span class="p">).</span><span class="nx">load</span><span class="p">(</span><span class="s1">&#39;/html/users.html #userListDiv&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC3'>		<span class="nx">$</span><span class="p">.</span><span class="nx">getJSON</span><span class="p">(</span><span class="s1">&#39;/users&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>			<span class="c1">// create the user list</span></div><div class='line' id='LC6'>			<span class="kd">var</span> <span class="nx">items</span> <span class="o">=</span> <span class="p">[</span> <span class="s1">&#39;&lt;ul&gt;&#39;</span><span class="p">];</span></div><div class='line' id='LC7'>			<span class="nx">$</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="nx">item</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC8'>				<span class="nx">items</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="s1">&#39;&lt;li&gt;&lt;a href=&quot;/users/&quot;&#39;</span> <span class="o">+</span> <span class="nx">item</span><span class="p">.</span><span class="nx">id</span> <span class="o">+</span> <span class="s1">&#39;&quot;&gt;&#39;</span></div><div class='line' id='LC9'>						<span class="o">+</span> <span class="nx">item</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="s1">&#39;&lt;/a&gt;&lt;/li&gt;&#39;</span><span class="p">);</span></div><div class='line' id='LC10'>			<span class="p">});</span></div><div class='line' id='LC11'>			<span class="nx">items</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="s1">&#39;&lt;/ul&gt;&#39;</span><span class="p">);</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>			<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="nx">items</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">);</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>			<span class="c1">// clear current user list</span></div><div class='line' id='LC16'>			<span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#userListDiv&#39;</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">);</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>			<span class="c1">// add new user list</span></div><div class='line' id='LC19'>			<span class="nx">$</span><span class="p">(</span><span class="nx">result</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s1">&#39;#userListDiv&#39;</span><span class="p">);</span></div><div class='line' id='LC20'>		<span class="p">});</span></div><div class='line' id='LC21'>	<span class="p">}</span></div><div class='line' id='LC22'><span class="p">};</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2100959/cffa6990804e2b0de1fb2b918bfd763fa2fb22c1/app_4.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2100959#file_app_4.js" style="float:right;margin-right:10px;color:#666">app_4.js</a>
            <a href="https://gist.github.com/2100959">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>If you find all the string concatenation going on in above snippet a bit sloppy, you can always use your favourite template plugin for jQuery. </p>
<p>Note: The <code>Object.extended</code> function in above snippets comes from the wonderful <a href="http://sugarjs.com">Sugar.js</a> library that makes working with native javascript objects a breeze.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2012/03/19/building-a-single-page-webapp-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Role based views using css and javascript</title>
		<link>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=role-based-views-using-css-and-javascript</link>
		<comments>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 07:40:24 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=157</guid>
		<description><![CDATA[In web applications, role based views are normally done using server side if..else tags etc. When developing pure javascript clients, one way of doing role based views is again using javascript conditionals. This approach suffers from the fact that role-related &#8230; <a href="http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In web applications, role based views are normally done using server side <code>if..else</code> <a href="http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html#d0e6296">tags</a> etc. When developing pure javascript clients, one way of doing role based views is again using javascript conditionals. This approach suffers from the fact that role-related code is scattered throughout the document. Another simple way to achieve the same is using css classes.</p>
<p>Let us assume that we have three roles, <code>ADMIN</code> and <code>USER</code> and <code>GUEST</code>. Then we will define three css classes as follows:</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nc">.role_admin</span><span class="o">,</span> <span class="nc">.role_user</span><span class="o">,</span> <span class="nc">.role_guest</span><span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">display</span><span class="o">:</span><span class="k">none</span><span class="p">;</span></div><div class='line' id='LC3'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/f8e3f87ba993b3f17cb7cac9404c8b2566263ed3/role.css" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role.css" style="float:right;margin-right:10px;color:#666">role.css</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now consider the following html doc, that has css classes attached to elements according to the current user&#8217;s role.</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;html&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;head&gt;</span></div><div class='line' id='LC3'><span class="nt">&lt;link</span> <span class="na">href=</span><span class="s">&quot;./role.css&quot;</span> <span class="na">rel=</span><span class="s">&quot;stylesheet&quot;</span> <span class="na">type=</span><span class="s">&quot;text/css&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC4'><span class="nt">&lt;/head&gt;</span></div><div class='line' id='LC5'><span class="nt">&lt;body&gt;</span></div><div class='line' id='LC6'><span class="nt">&lt;h1</span> <span class="na">class=</span><span class="s">&quot;role_admin&quot;</span><span class="nt">&gt;</span>This should be visible to admin only.<span class="nt">&lt;h1&gt;</span></div><div class='line' id='LC7'><span class="nt">&lt;h2</span> <span class="na">class=</span><span class="s">&quot;role_user&quot;</span><span class="nt">&gt;</span>This is for the normal logged in user.<span class="nt">&lt;/h1&gt;</span></div><div class='line' id='LC8'><span class="nt">&lt;h3</span> <span class="na">class=</span><span class="s">&quot;role_guest&quot;</span><span class="nt">&gt;</span>And this is for the guest.<span class="nt">&lt;/h1&gt;</span></div><div class='line' id='LC9'><span class="nt">&lt;/body&gt;</span></div><div class='line' id='LC10'><span class="nt">&lt;/html&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/a30ef0aa9a9852da64669667cfd8b3fd922dc6eb/roleSample.html" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role_sample.html" style="float:right;margin-right:10px;color:#666">roleSample.html</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>At this stage if you load the document in a browser, you will not see anything as nothing all the roles are invisible. So lets spruce this up with a bit of javascript to set proper css class properties.</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We are assuming that jQuery and jQuery Cookie plugin are included in the doc</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">role</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">cookie</span><span class="p">(</span><span class="s1">&#39;user_role&#39;</span><span class="p">);</span> <span class="c1">// Should return one of &#39;ADMIN&#39;, &#39;USER&#39; OR &#39;GUEST&#39;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// set property for relevant css class</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">cssClass</span> <span class="o">=</span> <span class="s1">&#39;.role_&#39;</span><span class="o">+</span> <span class="nx">role</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">();</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt; &quot;</span><span class="o">+</span> <span class="nx">cssClass</span> <span class="o">+</span> <span class="s2">&quot;{ display:block } &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC9'><span class="p">});</span></div><div class='line' id='LC10'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/d27ccc00e3b0607785bb11cec4d118a9ca62695b/role.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role.js" style="float:right;margin-right:10px;color:#666">role.js</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>If a cookie <code>role</code> is set that represents the current user&#8217;s role then adding the above snippet of code will enable all the elements tagged with css class <code>.role_{roleCookie}</code> to be visible.</p>
<p>Even though this is a very simple implementation, we can modify it easily to take into account more complex scenarios. For example, if you have completely ordered role based visibility (access level), i.e. given role R1 and R2, we can always tell which role has more visibility, then we can extend as follows:
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We are assuming that jQuery and jQuery Cookie plugin are included in the doc</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">role</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">cookie</span><span class="p">(</span><span class="s1">&#39;user_role&#39;</span><span class="p">);</span> <span class="c1">// Should return one of &#39;ADMIN&#39;, &#39;USER&#39; OR &#39;GUEST&#39;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// set property for relevant css class</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="p">(</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;ADMIN&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// make everything visible</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_admin, .role_user, .role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span> <span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">app</span><span class="p">.</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;USER&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// make user and guest part visible, admin part will remain invisible</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_user, .role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span> <span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">app</span><span class="p">.</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;GUEST&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// only show the guest part</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC19'><span class="p">});</span></div><div class='line' id='LC20'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/ca1c750910a67bc77f119a2551e42561337a85f6/roleOrdered.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role_ordered.js" style="float:right;margin-right:10px;color:#666">roleOrdered.js</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p><em>Caution:</em> One thing to note here is that all the parts of the document are sent to all the users, only what is visible on the browser is role based. So, server side authorisation checks will always be there as nothing stops a suspecting user from looking into the source and firing that dreaded request.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bing/Google toggle bookmarklet</title>
		<link>http://anandnalya.com/2009/10/06/binggoogle-toggle-bookmarklet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=binggoogle-toggle-bookmarklet</link>
		<comments>http://anandnalya.com/2009/10/06/binggoogle-toggle-bookmarklet/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 11:49:00 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[bing]]></category>
		<category><![CDATA[bookmarklet]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[ie8]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=100</guid>
		<description><![CDATA[I’ve been trying out Bing as my default search engine for last few months and the results are definitely at par with that of Google for most of the consumer categories. But sometimes you just need a quick toggle to &#8230; <a href="http://anandnalya.com/2009/10/06/binggoogle-toggle-bookmarklet/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I’ve been trying out <a href="http://bing.com/">Bing</a> as my default search engine for last few months and the results are definitely at par with that of Google for most of the consumer categories. But sometimes you just need a quick toggle to see what <a href="http://google.com/">Google</a> would return for your query. So here is a small bookmarklet that I wrote that toggles from Bing to Google and vice versa.</p>
<p>Drag this to you favourites bar: <a href="javascript:if(window.location.hostname.match(/.bing./))window.location.hostname='www.google.com';else if(window.location.hostname.match(/.google./))window.location.hostname='www.bing.com';">Bing/Google Toogle</a></p>
<p>Note: Its tested on IE8 only. A few tweaks might be required for Firefox.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2009/10/06/binggoogle-toggle-bookmarklet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Minifying Javascript/css without changing file references in your source</title>
		<link>http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=minifying-javascriptcss-without-changing-file-references-in-your-source</link>
		<comments>http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 13:21:02 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[compressor]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=79</guid>
		<description><![CDATA[Rule 10 of Steve Souders High Performance Web Sites: Minify Javascript The most common problem faced while implemnting this is how you handle the full and minified version and how to change there reference in referencing documents. One of the &#8230; <a href="http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Rule 10 of Steve Souders High Performance Web Sites: <a href="http://developer.yahoo.net/blog/archives/2007/07/high_performanc_8.html">Minify Javascript</a></p>
<p>The most common problem faced while implemnting this is how you handle the full and minified version and how to change there reference in referencing documents. One of the easier ways to do this is to make it part of the deployment process. </p>
<p>Here are the relevent steps involved.</p>
<p>I&#8217;m using <a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a>.<br />
<div id="gist-1124427" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">#!/bin/bash</span></div><div class='line' id='LC2'>&nbsp;</div><div class='line' id='LC3'><span class="c">#Execute this script after checking out the latest source from repository.</span></div><div class='line' id='LC4'>&nbsp;</div><div class='line' id='LC5'><span class="c">#Minify all javascript files</span></div><div class='line' id='LC6'><span class="nb">cd</span> /path/to/javascript</div><div class='line' id='LC7'><span class="k">for </span>x in <span class="sb">`</span>ls *.js<span class="sb">`</span></div><div class='line' id='LC8'><span class="k">do</span></div><div class='line' id='LC9'><span class="k">        </span>java -jar /path/to/compressor/yuicompressor-2.4.2.jar -o <span class="k">${</span><span class="nv">x</span><span class="p">%%.*</span><span class="k">}</span>-min.js --preserve-semi  <span class="nv">$x</span></div><div class='line' id='LC10'><span class="k">done</span></div><div class='line' id='LC11'>&nbsp;</div><div class='line' id='LC12'><span class="c">#Minfiy all css files</span></div><div class='line' id='LC13'><span class="nb">cd</span> /path/to/css</div><div class='line' id='LC14'><span class="k">for </span>x in <span class="sb">`</span>ls *.css<span class="sb">`</span></div><div class='line' id='LC15'><span class="k">do</span></div><div class='line' id='LC16'><span class="k">        </span>java -jar /path/to/compressor/yuicompressor-2.4.2.jar -o <span class="k">${</span><span class="nv">x</span><span class="p">%%.*</span><span class="k">}</span>-min.css  <span class="nv">$x</span></div><div class='line' id='LC17'><span class="k">done</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1124427/ad846053c02470535698614eed39c9401f5caa88/minify.sh" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1124427#file_minify.sh" style="float:right;margin-right:10px;color:#666">minify.sh</a>
            <a href="https://gist.github.com/1124427">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
<br />
Now you don&#8217;t want to replace all references to <em>x.css</em> or <em>x.js</em> in your development code with references to <em>x-min.css</em> and <em>x-min.js</em> respectively. So what you can do is rewrite all those filenames at the web server level.</p>
<p>For apache the following rewrite rules work fine:<br />
<div id="gist-1124427" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>#enable rewriting</div><div class='line' id='LC2'>RewriteEngine on</div><div class='line' id='LC3'>RewriteRule /(.*)\.js /$1-min.js</div><div class='line' id='LC4'>RewriteRule /(.*)\.css /$1-min.css</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1124427/0a7c12d3f31f779d173b821962361c0d2f176f65/httpd.conf" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1124427#file_httpd.conf" style="float:right;margin-right:10px;color:#666">httpd.conf</a>
            <a href="https://gist.github.com/1124427">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
<br />
<em>Caution:</em> Remember to delete existing minified css/js file before running the minifying script or you will end up with file names like x-min-min-min.js and so on. One way to do this is to clear the js/css folder before checking out files from your source repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

