<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Chorn Sokun's Weblog &#187; jQuery</title>
	<atom:link href="http://chornsokun.wordpress.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://chornsokun.wordpress.com</link>
	<description>try { divide and conquer; } catch { keep it simple!; } finally { nothing is impossible; }</description>
	<lastBuildDate>Thu, 05 Nov 2009 10:11:28 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='chornsokun.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/23c892e38e24abe8bd8ad0c8d763caf2?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Chorn Sokun's Weblog &#187; jQuery</title>
		<link>http://chornsokun.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://chornsokun.wordpress.com/osd.xml" title="Chorn Sokun&#8217;s Weblog" />
		<item>
		<title>Use jQuery to catch and display MR AJAX errors</title>
		<link>http://chornsokun.wordpress.com/2009/10/02/use-jquery-to-catch-and-display-mr-ajax-errors/</link>
		<comments>http://chornsokun.wordpress.com/2009/10/02/use-jquery-to-catch-and-display-mr-ajax-errors/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 17:58:56 +0000</pubDate>
		<dc:creator>Chorn Sokun</dc:creator>
				<category><![CDATA[MonoRail]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://chornsokun.wordpress.com/?p=541</guid>
		<description><![CDATA[This post influenced Dave Ward&#8217;s post to replicate the behavior that he discussed within MonoRail application.  To mimic this behavior I created a method call RenderJsonError(Exception ex) inside my base controller (I want to reuse this method)

public abstract class AbstractBaseController: ARSmartDispatcherController{
   private void RenderJsonError(Exception ex){
      CancelLayout();
      [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chornsokun.wordpress.com&blog=3780464&post=541&subd=chornsokun&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This post influenced <a href="http://encosia.com/2009/03/04/use-jquery-to-catch-and-display-aspnet-ajax-service-errors/" target="_blank">Dave Ward&#8217;s post</a> to replicate the behavior that he discussed within MonoRail application.  To mimic this behavior I created a method call RenderJsonError(Exception ex) inside my base controller (I want to reuse this method)</p>
<pre class="brush: csharp;">
public abstract class AbstractBaseController: ARSmartDispatcherController{
   private void RenderJsonError(Exception ex){
      CancelLayout();
      Response.StatusCode = 500;
      Response.ContentType = &quot;application/json; charset=utf-8&quot;;

      // I love this Framework :)
      var json = Context.Services.JSONSerializer.Serialize(
         new ErrorObject(){
            Message = e.Message,
            StackTrace = e.StackTrace
         });

      RenderText(json);
   }
}
</pre>
<p>From the code above look like I need to create a dummy DTO class ErrorObject and hand it over to JSONSerializer service instead of doing string escape/string concat myself :) MR use <em>Newtonsoft.Json.dll</em> behind the scene.</p>
<pre class="brush: csharp;">
public abstract class AbstractBaseController: ARSmartDispatcherController{
  ....
  internal class ErrorObject{
     public string Message { get; set; }
     public string StackTrace { get; set; }
  }
}
</pre>
<p>Now let through some booms back to client</p>
<pre class="brush: csharp;">
public class HomeController: AbstractBaseController{
   public void Index(){
      try{
          throw new Exception(&quot;Boom ! boom !&quot;);
      }catch(Exception ex){
          RenderJsonError(ex);
      }
   }
}
</pre>
<p>That work ! however if you don&#8217;t want to deal with try{..} catch {..} then combine this technique with RescueAttribute describe in my <a href="http://chornsokun.wordpress.com/2009/10/01/monorail-irescuecontroller-handle-unexpected-exception/">previous post</a>.</p>
 Tagged: jQuery, JSON <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chornsokun.wordpress.com/541/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chornsokun.wordpress.com/541/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chornsokun.wordpress.com/541/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chornsokun.wordpress.com/541/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chornsokun.wordpress.com/541/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chornsokun.wordpress.com/541/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chornsokun.wordpress.com/541/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chornsokun.wordpress.com/541/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chornsokun.wordpress.com/541/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chornsokun.wordpress.com/541/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chornsokun.wordpress.com&blog=3780464&post=541&subd=chornsokun&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://chornsokun.wordpress.com/2009/10/02/use-jquery-to-catch-and-display-mr-ajax-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fc60b386b64b2b05c043d7676dde360e?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">c.sokun</media:title>
		</media:content>
	</item>
		<item>
		<title>MonoRail &amp; jQuery</title>
		<link>http://chornsokun.wordpress.com/2009/03/26/monorail-jquery/</link>
		<comments>http://chornsokun.wordpress.com/2009/03/26/monorail-jquery/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 14:13:37 +0000</pubDate>
		<dc:creator>Chorn Sokun</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[MonoRail]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://chornsokun.wordpress.com/?p=455</guid>
		<description><![CDATA[Last year I blog about using jQuery.ajaxForm() to send http post request to MonoRail action and finally it depend on JSGenerator to produce javascript and send it back to client.
But overtime I find it pretty scary using that approach:

It give too much control to the server &#8211; the server can generate a bunch of javascript [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chornsokun.wordpress.com&blog=3780464&post=455&subd=chornsokun&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Last year I <a href="http://chornsokun.wordpress.com/2008/09/21/jquery-ajaxform-generate-on-the-fly/">blog </a>about using jQuery.ajaxForm() to send http post request to MonoRail action and finally it depend on JSGenerator to produce javascript and send it back to client.</p>
<p>But overtime I find it pretty scary using that approach:</p>
<ul>
<li>It give too much control to the server &#8211; the server can generate a bunch of javascript and send off to the client sort of invade client responsibility.</li>
<li>In some scenario I even specify from the server which div element I want to render my data table in</li>
</ul>
<pre class="brush: csharp;">
  page.ReplaceHtml('#divId', &quot;content_to_be_replace_from_server&quot;)
</pre>
<ul>
<li>using approach in previous post I was not be able to ensure 100% that if I send piece of jquery script that it combine into the main DOM properly for instance I did have a form &amp; a jQuery to confirm the form into ajaxForm but that behavior is not always reliable.</li>
</ul>
<p>So what the deal now? well, I happen to change the strategy a while ago just haven&#8217;t got time to blog about it the solution is pretty simple &#8220;Don&#8217;t make JSGenerator your new hammer.&#8221;, Let see my simplify approach:</p>
<pre class="brush: csharp;">
&lt;div id='dlist'&gt;
  &lt;!-- I am going to render list of data in this div --&gt;&lt;/div&gt;
&lt;input type=&quot;button&quot; value=&quot;Show data&quot; onclick=&quot;showData();&quot; /&gt;
&lt;input type=&quot;button&quot; value=&quot;Add more&quot; onclick=&quot;addData();&quot; /&gt;
&lt;div id='dform' style='display: none;'&gt;
  &lt;!-- I am going to load form into this div --&gt;&lt;/div&gt;
</pre>
<p>That pretty normal HTML markup right? 2 div one for display list of data second one for display form for adding more data and I also have 2 buttons to invoke client script (jQuery).</p>
<pre class="brush: csharp;">
&lt;script&gt;
function showData(){
   $('#dlist').load( '/task/list.rails', {'status': 'incomplete'} );
}

function addData(){
   $('#dform').load( '/task/add.rails', {}, function(){
      $('#dform').show();
    });
}
&lt;/script&gt;
</pre>
<p>Now that instead of using $.ajax I use $(&#8216;#elementId&#8217;).<a href="http://docs.jquery.com/Ajax/load" target="_blank">load</a>() instead because load() will inject html render by MonoRail action in the DOM and it just as native element.</p>
<p><strong>Note</strong> : line 07 <em>dform </em>had style apply so that without having form inside it don&#8217;t occupy space in the page however one content loaded we need to set visibility for it.</p>
<p>For your reference the final piece of the puzzle is purely view code like this (View\task\add.brail)</p>
<pre class="brush: xml;">
&lt;form method='post' id='taskForm' action='/task/create.rail'&gt;
  Name: &lt;input type='text' name='t.Name' /&gt;
  &lt;input type='submit' value='Create' /&gt;
&lt;/form&gt;

&lt;!-- not inject some jQuery script along make the form submit via ajax --&gt;
&lt;script&gt;
   $(function(){
       $('#taskForm').ajaxForm ({
          success: function(response){
            $('#dlist').html(response);
          },
          error: function (xhr, status, error) {} // handle error if you wish
        });
   });
&lt;/script&gt;
</pre>
<p>Notice on line #09 : need the form to submit using ajax and line #11 the client take control where it want to place the result (list of task).</p>
<p>It seem like we have to write a bit more code (jQuery) but after all that the fun part of being a programmer  write code that :) Hope this would help.</p>
 Tagged: Ajax, jQuery <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chornsokun.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chornsokun.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chornsokun.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chornsokun.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chornsokun.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chornsokun.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chornsokun.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chornsokun.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chornsokun.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chornsokun.wordpress.com/455/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chornsokun.wordpress.com&blog=3780464&post=455&subd=chornsokun&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://chornsokun.wordpress.com/2009/03/26/monorail-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fc60b386b64b2b05c043d7676dde360e?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">c.sokun</media:title>
		</media:content>
	</item>
		<item>
		<title>jQuery ajaxform generate on the fly</title>
		<link>http://chornsokun.wordpress.com/2008/09/21/jquery-ajaxform-generate-on-the-fly/</link>
		<comments>http://chornsokun.wordpress.com/2008/09/21/jquery-ajaxform-generate-on-the-fly/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 14:27:25 +0000</pubDate>
		<dc:creator>Chorn Sokun</dc:creator>
				<category><![CDATA[OSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Castle]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MonoRail]]></category>

		<guid isPermaLink="false">http://chornsokun.wordpress.com/?p=308</guid>
		<description><![CDATA[What scripting language would come to your mind first when you begin to talk about client-side scripting (web)? Don&#8217;t tell me it is VBScript or I&#8217;ll LOL, and so your answer would be? &#8220;JavaScript&#8221; alright +1 from me :). I believe most of the programmer who is reading this post knowing/aware of jQuery.
I can extend the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chornsokun.wordpress.com&blog=3780464&post=308&subd=chornsokun&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>What scripting language would come to your mind first when you begin to talk about client-side scripting (web)? Don&#8217;t tell me it is VBScript or I&#8217;ll LOL, and so your answer would be? &#8220;JavaScript&#8221; alright +1 from me :). I believe most of the programmer who is reading this post knowing/aware of <a href="http://www.jquery.com" target="_blank">jQuery</a>.</p>
<p>I can extend the talk about jQuery into a series of posts but I am not gonna do that because <a href="http://www.google.com/search?q=jquery" target="_blank">google</a> would give you millions of result about jQuery and it usage.</p>
<p>Today what I am interested to talk about is using jQuery ajax functionality in conjunction with Castle.MonoRail to generate HTML Form on the fly and the form will also has ajax functionality. What does it mean? well let us all get into application context of adding new customer. In general we are going to have a customer page <em>/customers</em> which list all existing customers in a table format. Additional customer can be added by clicking on <em>Add New Customer</em> link wired up with a javascript function.</p>
<pre class="brush: xml;">

&lt;a href='#' onclick='addNewCustomer()'&gt;Add New Customer&lt;/a&gt;
</pre>
<p>and the <em>addNewCustomer()</em> function looks like this:</p>
<pre class="brush: jscript;">
&lt;script language=&quot;javascript&quot;&gt; 

function addNewCustomer(){

   $.ajax({
      dataType: 'script',
      url: '/customers/addNewCustomer'
   });

}

&lt;/script&gt;
</pre>
<p>From the above script the web browser going to send off a request to <em>/customers/addNewCustomer</em> which is going to handle by an action of CustomersController. If you care to see the action code it look like this:</p>
<pre class="brush: csharp;">

[Layout(&quot;default&quot;), Rescue(&quot;generalerror&quot;)]
public class CustomerController: SmartDispatcherController
{
    .... code removed for clarity 

    public void AddNewCustomer(){

        PropertyBag[&quot;c&quot;] = new Customer();

    }

    .... code removed for clarity

}
</pre>
<p>and the rest of the work will be left over to the view engine to find the view template file and process it. Since I am a big fan of <a href="http://www.castleproject.org/monorail/documentation/trunk/viewengines/brail/index.html" target="_self">Brail View Engine</a> the template file would be call <em>AddNewCustomer.brail </em>and the code look like this:</p>
<pre class="brush: xml;">

&lt;form id='form1' method='post' action='/customer/create'&gt;

   Name: ${Form.TextField(&quot;c.Name&quot;)}
   Phone: ${Form.TextField(&quot;c.Phone&quot;)}
   &lt;input type='Submit' value='Create New' /&gt;

&lt;/form&gt;

&lt;script type='text/javascript'&gt;

   $(document).ready(function(){

      $('#form1').ajaxForm( { dataType: 'script' } );

   });

&lt;/script&gt;
</pre>
<p>Again it just simple HTML markup mixed with a few Brail&#8217;s tagging macro but take a closer look at the javascript code at bottom, what does it do? Well the idea is once the template is processed it going to generate a pure HTML form and send it back to the client side, however the javascript that send along will ensure that the new generated HTML form will perform a post back via jQuery ajax functionality. Hmm, sound cool. But didn&#8217;t I just waste a bytes of data with this approach? I tell you what, this application is going to run on a <a href="http://en.wikipedia.org/wiki/LAN" target="_blank">LAN</a> so bandwidth is not a problem.</p>
<p>Up to this point everything works perfect, <strong>except</strong> for one final click on the Submit button to save new customer and oop! look there is a post without ajax <strong>It just work as normal as any other HTML form submission</strong>. Hmm, what I had discovered was by generating form id=&#8217;something&#8217; or whatever as long as it is not id=&#8217;form1&#8242;. The submission of the form will route through ajax weir isn&#8217;t it? I haven&#8217;t investigate into jQuery code just yet jQuery might use <em>form1 </em>for something else internally, do you know what it is?</p>
<p><strong>UPDATE 2008-11-25:</strong></p>
<p>I believe my first assumption about form id attribute which cause ajax not to fire was wrong. What I recently found was if I am not calling $(document).ready(&#8230;); in the original page the generated dom pass in even if it contain $(document).ready(&#8230;) script block that block just do nothing.</p>
 Tagged: Castle, jQuery, MonoRail <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chornsokun.wordpress.com/308/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chornsokun.wordpress.com/308/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chornsokun.wordpress.com/308/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chornsokun.wordpress.com/308/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chornsokun.wordpress.com/308/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chornsokun.wordpress.com/308/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chornsokun.wordpress.com/308/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chornsokun.wordpress.com/308/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chornsokun.wordpress.com/308/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chornsokun.wordpress.com/308/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chornsokun.wordpress.com&blog=3780464&post=308&subd=chornsokun&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://chornsokun.wordpress.com/2008/09/21/jquery-ajaxform-generate-on-the-fly/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fc60b386b64b2b05c043d7676dde360e?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">c.sokun</media:title>
		</media:content>
	</item>
	</channel>
</rss>