<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>waelchatila.com - Web category</title>
  <link>http://waelchatila.com:80/categories/web/</link>
  <description>Notes on Software, Engineering and Science</description>
  <language>en</language>
  <copyright>Wael Chatila</copyright>
  <lastBuildDate>Fri, 23 May 2008 05:27:00 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  
  <item>
    <title>Single Table Polymorphic Inheritance with ActiveRecords </title>
    <link>`</link>
    
      
        <description>
          Implementing a single table polymorphic inheritance in rails is dead easy. 

Simply do
&lt;div class=&#034;codeSample&#034;&gt;
class Fruit &lt; ActiveRecord::Migration
  def self.up
    create_table :persons do |t|
      t.column :type :string
      t.column :shape, :string
      t.timestamps
    end

  end

  def self.down
    drop_table :fruits
  end
end
&lt;/div&gt;
or if you a migrating...
&lt;div class=&#034;codeSample&#034;&gt;
class Fruit &lt; ActiveRecord::Migration
    def self.up
        add_column :fruits, :type, :string
    end

    def self.down
        remove_column :fruits, :type
    end
end
&lt;/div&gt;

Now, all you have to do is to create your sub-classes
&lt;div class=&#034;codeSample&#034;&gt;
class Fruit &lt; ActiveRecord::Base
    def calc_volume
       ...
    end
end

class Apple &lt; Fruit
    def calc_volume
       ...
    end
end

&lt;/div&gt;

Don&#039;t forget to put &lt;i&gt;Apple&lt;/i&gt; in a separate file by itself &lt;i&gt;apple.rb&lt;/i&gt; or else ruby will spit out an unconditional warning if you reference &lt;i&gt;Apple&lt;/i&gt; in your code with &lt;i&gt;Fruit::Apple&lt;/i&gt;
&lt;div class=&#034;codeSample&#034;&gt;
warning: toplevel constant Fruit referenced by Fruit::Apple

&lt;/div&gt;
&lt;p&gt;&lt;a href=&#034;`&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;
        </description>
      
      
    
    
    
    <category>Web</category>
    
    <comments>http://waelchatila.com:80/2008/05/22/1211520420000.html#comments</comments>
    <guid isPermaLink="true">`</guid>
    <pubDate>Fri, 23 May 2008 05:27:00 GMT</pubDate>
  </item>
  
  <item>
    <title>Internet Explorer 6 &amp; 7 document.domain bug</title>
    <link>http://waelchatila.com:80/2007/10/31/1193851500000.html</link>
    
      
        <description>
          &lt;b&gt;Disclaimer:&lt;/b&gt; Im not sure this a bug discovery...couldn&#039;t find anything that resembled it after a couple of minutes of googling, or a super silly security restriction.
&lt;p&gt;
I&#039;ve been coding some javascript ajax code that inserts an iframe, and into the created iframe, inserts a form and submits the form to a (cross domain) server. This is an old cross-domain ajax technique by now, but while implementing it I came across this ridiculously annoying bug in IE. The line in red below will result in an &#034;Access Denied&#034; exception being thrown...again, only in IE. Tried this with IE6 under WinXP Media Center Edition SP2, IE7 under WinXP Home Edition SP2, and IE7 Windows Vista Home. Needless to say...it works just fine in FireFox.

&lt;div class=&#034;codeSample&#034;&gt;
&amp;lt;script type=&#034;text/javascript&#034;&amp;gt;
  document.domain = &#039;waelchatila.com&#039;;
  var new_html = &#039;&amp;lt;iframe id=&#034;frm&#034; name=&#034;frm&#034; src=&#034;http://waelchatila.com&#034;&amp;gt;&amp;lt;/iframe&amp;gt;&#039;;
  document.body.innerHTML+=new_html;
  try {
    var iframe_doc = &lt;font color=&#034;red&#034;&gt;document.getElementById(&#034;frm&#034;).contentWindow.document;&lt;/font&gt;
  } catch (e) {
    alert(e);
  }
&amp;lt;/script&amp;gt;
&lt;/div&gt;
&lt;div id=&#034;frm1&#034;&gt;&lt;/div&gt;
&lt;script type=&#034;text/javascript&#034;&gt;
function insertIframe() {
        document.domain = &#039;waelchatila.com&#039;;
            var new_html = &#039;&lt;iframe id=&#034;frm&#034; name=&#034;frm&#034; src=&#034;http://waelchatila.com&#034;&gt;&lt;/iframe&gt;&#039;;
            document.getElementById(&#039;frm1&#039;).innerHTML+=new_html;
var bError = false;
try {
 var iframe_doc = document.getElementById(&#034;frm&#034;).contentWindow.document;
} catch (e) {
   bError = true;
}
alert(bError?&#034;Exception thrown&#034;:&#034;No exceptions thrown&#034;);
}
    &lt;/script&gt;


Try it by clicking the button below. &lt;p&gt;
&lt;button onclick=&#034;insertIframe();return false;&#034;&gt;Try it!&lt;/button&gt;
&lt;p&gt;
This page should be served off &lt;i&gt;http://waelchatila.com&lt;/i&gt;. As you can see, the document.domain is explicitly set to the domain the page was served from. The iframe src is also from the same server. I&#039;ve tried it with a whole variety of variations, none of the demonstrated here but tested for.
&lt;ul&gt;
&lt;li&gt;iframe&#039;s document.domain = &#039;waelchatila.com&#039;;&lt;/li&gt;
&lt;li&gt;iframe src is empty&lt;/li&gt;
&lt;li&gt;iframe src is &#034;javascript:false&#034;&lt;/li&gt;
&lt;/ul&gt;
There should be no problem accessing the iframe&#039;s content in either case. 
&lt;p&gt;Now, if the &lt;i&gt;document.domain = &#039;waelchatila.com&#039;;&lt;/i&gt; line is removed, it works. In order for this to work, you&#039;ll need to reload this page if you clicked the previous button since document.domain has now already been assigned.

&lt;p&gt;
&lt;div id=&#034;frm2&#034;&gt;&lt;/div&gt;
&lt;script type=&#034;text/javascript&#034;&gt;
function insertIframe2() {
            var new_html = &#039;&lt;iframe id=&#034;frm&#034; name=&#034;frm&#034; src=&#034;http://waelchatila.com&#034;&gt;&lt;/iframe&gt;&#039;;
            document.getElementById(&#039;frm2&#039;).innerHTML+=new_html;
var bError = false;
try {
 var iframe_doc = document.getElementById(&#034;frm&#034;).contentWindow.document;
} catch (e) {
   bError = true;
}
alert(bError?&#034;Exception thrown&#034;:&#034;No exceptions thrown&#034;);
}
    &lt;/script&gt;


&lt;button onclick=&#034;insertIframe2();return false;&#034;&gt;Try it!&lt;/button&gt;
&lt;p&gt;
The same results present itself if the iframe is created with &lt;i&gt;document.createElement(&#039;iframe&#039;)&lt;/i&gt; and &lt;i&gt;document.appendChild(...)&lt;/i&gt;
&lt;p&gt;
I&#039;ve yet to find a workaround for this issue. I&#039;ll be positing another entry if I found out how. Please let me know if you know of one!
        </description>
      
      
    
    
    
    <category>AJAX</category>
    
    <category>Windows</category>
    
    <category>Web</category>
    
    <comments>http://waelchatila.com:80/2007/10/31/1193851500000.html#comments</comments>
    <guid isPermaLink="true">http://waelchatila.com:80/2007/10/31/1193851500000.html</guid>
    <pubDate>Wed, 31 Oct 2007 17:25:00 GMT</pubDate>
  </item>
  
  <item>
    <title>DisplayTag Paging</title>
    <link>http://waelchatila.com:80/2006/11/07/1162942661799.html</link>
    
      
      
        <description>
          &lt;a href=&#034;http://displaytag.sourceforge.net&#034;&gt;DisplayTag&lt;/a&gt; is a very versatile jsp tag library. It provides a whole lot for free. Paging, sorting, even / odd highlighting, grouping, exporting to PDF, Excel, CSV, XML, just to mention a few things it can do.  &lt;br /&gt;
I use it in pretty much every project. However, I&#039;ve yet to see a built in way to keep track of which page you were at last. In this blog entry I explain one general way to get around this limitation.&lt;p&gt;&lt;a href=&#034;http://waelchatila.com:80/2006/11/07/1162942661799.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;
          &lt;p&gt;&lt;a href="http://waelchatila.com:80/2006/11/07/1162942661799.html"&gt;Read more...&lt;/a&gt;&lt;/p&gt;
        </description>
      
    
    
    
    <category>Java</category>
    
    <category>Web</category>
    
    <comments>http://waelchatila.com:80/2006/11/07/1162942661799.html#comments</comments>
    <guid isPermaLink="true">http://waelchatila.com:80/2006/11/07/1162942661799.html</guid>
    <pubDate>Tue, 07 Nov 2006 23:37:41 GMT</pubDate>
  </item>
  
  <item>
    <title>W AJAX design pattern </title>
    <link>http://waelchatila.com:80/2006/07/08/1152426781706.html</link>
    
      
        <description>
          This is a new design pattern which was independently discovered by me and &lt;a href=&#034;http://www.jlamp.com&#034; rel=&#034;nofollow&#034;&gt;Will Glass&lt;/a&gt; over the phone one day while discussing a painful loading time of around 7 seconds for a web application we were working on.
&lt;h2&gt;Describing the problem&lt;/h2&gt;
The web app front basically consists of a Google Map plus a form. The user can search for an address/place or, interactively on the map, select a boundary area for the search. When the search is submitted to the webserver, the webserver sends off an additional request to a 3rd party geodata server for the purpose of retrieving geocoding data plus demographical data. The problem is that this request to the 3rd party geocoding provider takes some time. The end result would be a wait for about 7 seconds between the time the search was submitted and the end resulting page showed up. The picture below depicts what&amp;rsquo;s going on &lt;center&gt;&lt;img alt=&#034;&#034; src=&#034;images/without_w.png&#034; /&gt;&lt;/center&gt; 1. User submits a search request with HTTP POST&lt;br /&gt;
2. A blocking request to the geodata server is issued&lt;br /&gt;
4. Geodata retrieved and results are sent back to the browser&lt;br /&gt;
5. Data received by browser&lt;br /&gt;
6. Browser finished processing and rendering the data and page fully displayed.&lt;br /&gt;
&lt;h2&gt;W AJAX Solution&lt;/h2&gt;
A way to reduce the response time is to utilize some AJAX. The idea is the following. When the search request arrives at the webserver, a new thread associated with the http session is spawned with the task to asynchronously do the 3rd party geodata request. Control is immediately returned back to the browser with no geodata. The browser, upon getting the no geocode data immediately sends of an AJAX request (asynchronous by nature) while it continues to render the web page with images, the Google Map, tables etc. This rendering takes some time to happen and by the time the rendering is done the AJAX request has landed at the webserver and is doing a blocking wait for the spawned geodata server request. When the 3rd party request is done, data is very quickly returned back to the browser (once again one of the natures of AJAX) and the rendered page is updated using DHTML. All in all reducing loading time, &lt;em&gt;but more importantly reducing perceived responsiveness for the user&lt;/em&gt;. &lt;br /&gt;
The picture below with accompanying steps describes more in detail how it works. &lt;center&gt;&lt;img alt=&#034;&#034; src=&#034;images/with_w.png&#034; /&gt;&lt;/center&gt; 1. User submits a search request with HTTP POST&lt;br /&gt;
2. A non-blocking request to the geodata server is issued in a spawned thread that is associated with the session. A web page layout with no geo data is returned to the browser.&lt;br /&gt;
3. Browser sends of an AJAX request with the purpose of retrieving geo data. Browser continues rendering the web page layout and elements.&lt;br /&gt;
4. AJAX request arrives at webserver and does a blocking wait for geo data to arrive. Geo data is finally calculated and results are sent back to the webserver. Webserver relays geo data to the AJAX request which returns.&lt;br /&gt;
5. Data is received by browser and presented in a quick manner using DHTML&lt;br /&gt;
&lt;h2&gt;Did it really work?&lt;/h2&gt;
Was the rendering that bad? and is the extra complexity worth it? It depends on the situation and application. In our case  we were able to squeeze down response time to around 3 seconds with the end result of a happy customer, in other words, it made a huge difference for us.
&lt;h2&gt;Why &amp;quot;W&amp;quot;?&lt;/h2&gt;
The calling pattern traced out in the image above resembles a &amp;quot;W&amp;quot; and an &amp;quot;U&amp;quot;, combine this with how &amp;quot;W&amp;quot; is pronounced in English and you get one visual &amp;quot;W&amp;quot; and, at least, one phonetic &amp;quot;U&amp;quot;. Besides, it&#039;s arguable the best way to start a name, what do you think Will? :)
        </description>
      
      
    
    
    
    <category>AJAX</category>
    
    <category>Web</category>
    
    <comments>http://waelchatila.com:80/2006/07/08/1152426781706.html#comments</comments>
    <guid isPermaLink="true">http://waelchatila.com:80/2006/07/08/1152426781706.html</guid>
    <pubDate>Sun, 09 Jul 2006 06:33:01 GMT</pubDate>
  </item>
  
  <item>
    <title>Javascript Atbash Decrypter/Encrypter</title>
    <link>http://waelchatila.com:80/2006/05/07/1147068107303.html</link>
    
      
        <description>
          Simply type the text you want to decrypt or encrypt in the text box below. The decrypted/encrypted text will appear as you type. &lt;script language=&#034;Javascript&#034;&gt;
	
	function atbash(str) {
		var ret = &#039;&#039;;
		for (i=0;i&lt;str.length;i++) {
			ret += String.fromCharCode(155 -  str[i].toUpperCase().charCodeAt(0));
		}
		return ret;
	}
	
        function solve(str) {
document.getElementById(&#039;atbash&#039;).innerHTML = atbash(str);

}
&lt;/script&gt; &lt;noscript&gt;You must have a browser that supports javascript , or javascript is currently disabled in your browser.&lt;/noscript&gt;
&lt;table&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;input type=&#034;text&#034; onkeyup=&#034;solve(this.value);&#034; /&gt;&lt;/td&gt;
            &lt;td id=&#034;atbash&#034;&gt;&amp;nbsp;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;h2&gt;Source Code&lt;/h2&gt;
&lt;div class=&#034;codeSample&#034;&gt; &amp;lt;script language=&#034;Javascript&#034;&amp;gt;
	function atbash(str) {
		var ret = &#039;&#039;;
		for (i=0;i&amp;lt;str.length;i++) {
			ret += String.fromCharCode(155 - str[i].toUpperCase().charCodeAt(0));
		}
		return ret;
	}
}
&amp;lt;/script&amp;gt; &lt;/div&gt;
        </description>
      
      
    
    
    
    <category>Games &amp; Fun</category>
    
    <category>Web</category>
    
    <comments>http://waelchatila.com:80/2006/05/07/1147068107303.html#comments</comments>
    <guid isPermaLink="true">http://waelchatila.com:80/2006/05/07/1147068107303.html</guid>
    <pubDate>Mon, 08 May 2006 06:01:47 GMT</pubDate>
  </item>
  
  </channel>
</rss>
