<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>duncanMgunn.com - Javascript</title>
    <link>http://www.duncangunn.me.uk/dasblog/</link>
    <description />
    <language>en-us</language>
    <copyright>Duncan M Gunn</copyright>
    <lastBuildDate>Sat, 15 May 2010 10:04:32 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>dasblog@example.com</managingEditor>
    <webMaster>dasblog@example.com</webMaster>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=9c72d486-9a78-4675-b388-db59605ed09f</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,9c72d486-9a78-4675-b388-db59605ed09f.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,9c72d486-9a78-4675-b388-db59605ed09f.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=9c72d486-9a78-4675-b388-db59605ed09f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Playing around with AJAX and returning
data via JSON, I was always getting the error handler called in my AJAX call, despite
getting data back from the server.<br /><br />
I noticed that my server code, which I'd quickly stolen from another blog, returned
the JSON like this:<br /><br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"> oBuilder.Append(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"{"</span>);
oBuilder.AppendFormat(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"'{0}'
: {1}"</span>, <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Minute"</span>, <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"500"</span>);
oBuilder.Append(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"}"</span>);</span></pre><br />
so this would return<br />
 { 'minute' : 500 }<br /><br />
All good right?<br /><br />
Wrong.  Jquery 1.4 now will parse valid JSON only, and that means the single
quotes inside that JSON packet HAVE to be double quotes.<br /><br /><br /><br /><br /><br /><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=9c72d486-9a78-4675-b388-db59605ed09f" /></body>
      <title>Invalid JSON - has to be double quotes</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,9c72d486-9a78-4675-b388-db59605ed09f.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2010/05/15/InvalidJSONHasToBeDoubleQuotes.aspx</link>
      <pubDate>Sat, 15 May 2010 10:04:32 GMT</pubDate>
      <description>Playing around with AJAX and returning data via JSON, I was always getting the error handler called in my AJAX call, despite getting data back from the server.&lt;br&gt;
&lt;br&gt;
I noticed that my server code, which I'd quickly stolen from another blog, returned
the JSON like this:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt; oBuilder.Append(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"{"&lt;/span&gt;);
oBuilder.AppendFormat(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"'{0}'
: {1}"&lt;/span&gt;, &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Minute"&lt;/span&gt;, &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"500"&lt;/span&gt;);
oBuilder.Append(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"}"&lt;/span&gt;);&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
so this would return&lt;br&gt;
&amp;nbsp;{ 'minute' : 500 }&lt;br&gt;
&lt;br&gt;
All good right?&lt;br&gt;
&lt;br&gt;
Wrong.&amp;nbsp; Jquery 1.4 now will parse valid JSON only, and that means the single
quotes inside that JSON packet HAVE to be double quotes.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=9c72d486-9a78-4675-b388-db59605ed09f" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,9c72d486-9a78-4675-b388-db59605ed09f.aspx</comments>
      <category>Javascript</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=be22c897-92c4-4869-9cb5-71bc1ed75667</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,be22c897-92c4-4869-9cb5-71bc1ed75667.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,be22c897-92c4-4869-9cb5-71bc1ed75667.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=be22c897-92c4-4869-9cb5-71bc1ed75667</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <a href="http://stackoverflow.com/questions/324771/consuming-remote-web-services-from-client-side-script">http://stackoverflow.com/questions/324771/consuming-remote-web-services-from-client-side-script</a>
        <br />
        <br />
        <i>"From what I understand, due to the "same origin policy" enforcement in current
browsers, it's impossible to obtain data from an XmlHttpRequest sent to a different
domain than the Javascript's original domain.<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span class="Apple-style-span" style="border-collapse: collapse; font-family: Arial,'Liberation Sans','DejaVu Sans',sans-serif; font-size: 14px; line-height: 18px; text-align: left;"><p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;">
I have close to zero experience regarding this matter, so I'm confused about web services
being unusable from Javascript. Does it mean that web applications with Ajax functionality
can only interact with themselves without calling services provided by other domains
? How do "mash-ups" work ? I guess the services are consumed server-side, then the
data is passed to the client via local Ajax calls. I don't know.
</p><p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;">
The only way I can imagine to achieve client-side consuming of services would be to
retrieve a Javascript file directly from the target web service's domain via a<span class="Apple-converted-space"></span><code style="border-width: 0px; margin: 0px; padding: 1px 5px; font-size: 14px; vertical-align: baseline; background-color: rgb(238, 238, 238); font-family: Consolas,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New';">script</code><span class="Apple-converted-space"></span>tag,
then use its API to interact with the remote domain.
</p><p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;">
Can anyone enlighten me ?"
</p><p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span class="Apple-style-span" style="border-collapse: collapse; font-family: Arial,'Liberation Sans','DejaVu Sans',sans-serif; font-size: 14px; line-height: 18px; text-align: left;"><p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;">
In your question your mentioned the &lt;script&gt; trick. JSONP is based on that.<span class="Apple-converted-space"> </span><a href="http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/" rel="nofollow" style="border-width: 0px; margin: 0px; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(0, 119, 204); text-decoration: none; cursor: pointer;">It
was formally proposed almost 3 years ago by Bob Ippolito</a>. It doesn't give you
the right to talk to the origin of the script — the origin is defined by your web
page, not by what else it includes. It works only because the server wraps JSON in
a calback function, which should be defined in your code, and will be executed by
&lt;script&gt; when loaded. Most famous example of JSONP would be Yahoo services,
including Flickr.
</p><p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;">
Another technique is to use window.name to transfer the information.<span class="Apple-converted-space"> </span><a href="http://www.sitepen.com/blog/2008/07/22/windowname-transport/" rel="nofollow" style="border-width: 0px; margin: 0px; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(0, 119, 204); text-decoration: none; cursor: pointer;">This
technique was detailed by Kris Zyp four month ago</a>. Additionally his article compares
window.name transport with JSONP. I don't know any high-profile service provider that
supports this new transport. Obviously it will change over time.
</p><p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;">
Of course, I should mention the upcoming<span class="Apple-converted-space"> </span><a href="http://msdn.microsoft.com/en-us/library/cc288060%28VS.85%29.aspx" rel="nofollow" style="border-width: 0px; margin: 0px; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(0, 119, 204); text-decoration: none; cursor: pointer;">Microsoft
XDomainRequest</a>. It is being planned to be shipped with IE8, and no other vendors
committed to support it, but it was presented for the inclusion in HTML 5. XDR is
a useful piece of functionality, but I suspect it'll be changed several times before
being accepted.
</p><p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;">
If you looked in the links you probably know by now that all these methods require
a certain level of cooperation from a 3rd-party server. You cannot use random services
at will. If you do have to use an uncooperative service, the only solution is to proxy
it through your own server with all associated problems: the questionable legality,
the reduced performance, the increased load on your server, the reduced number of
connections between user's browser and your server, and so on.
</p></span></span></p><p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"><br /></p></span></span></i>
        <p>
        </p>
        <img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=be22c897-92c4-4869-9cb5-71bc1ed75667" />
      </body>
      <title>Really good explanation of Same Origin Policy with real-world mash-up scenario</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,be22c897-92c4-4869-9cb5-71bc1ed75667.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2010/02/04/ReallyGoodExplanationOfSameOriginPolicyWithRealworldMashupScenario.aspx</link>
      <pubDate>Thu, 04 Feb 2010 11:58:30 GMT</pubDate>
      <description>&lt;a href="http://stackoverflow.com/questions/324771/consuming-remote-web-services-from-client-side-script"&gt;http://stackoverflow.com/questions/324771/consuming-remote-web-services-from-client-side-script&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
&lt;i&gt;"From what I understand, due to the "same origin policy" enforcement in current
browsers, it's impossible to obtain data from an XmlHttpRequest sent to a different
domain than the Javascript's original domain.&lt;span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; font-family: Arial,'Liberation Sans','DejaVu Sans',sans-serif; font-size: 14px; line-height: 18px; text-align: left;"&gt;
&lt;p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"&gt;
I have close to zero experience regarding this matter, so I'm confused about web services
being unusable from Javascript. Does it mean that web applications with Ajax functionality
can only interact with themselves without calling services provided by other domains
? How do "mash-ups" work ? I guess the services are consumed server-side, then the
data is passed to the client via local Ajax calls. I don't know.
&lt;/p&gt;
&lt;p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"&gt;
The only way I can imagine to achieve client-side consuming of services would be to
retrieve a Javascript file directly from the target web service's domain via a&lt;span class="Apple-converted-space"&gt; &lt;/span&gt;&lt;code style="border-width: 0px; margin: 0px; padding: 1px 5px; font-size: 14px; vertical-align: baseline; background-color: rgb(238, 238, 238); font-family: Consolas,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New';"&gt;script&lt;/code&gt;&lt;span class="Apple-converted-space"&gt; &lt;/span&gt;tag,
then use its API to interact with the remote domain.
&lt;/p&gt;
&lt;p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"&gt;
Can anyone enlighten me ?"
&lt;/p&gt;
&lt;p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"&gt;
&lt;span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; font-family: Arial,'Liberation Sans','DejaVu Sans',sans-serif; font-size: 14px; line-height: 18px; text-align: left;"&gt;
&lt;p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"&gt;
In your question your mentioned the &amp;lt;script&amp;gt; trick. JSONP is based on that.&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/" rel="nofollow" style="border-width: 0px; margin: 0px; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(0, 119, 204); text-decoration: none; cursor: pointer;"&gt;It
was formally proposed almost 3 years ago by Bob Ippolito&lt;/a&gt;. It doesn't give you
the right to talk to the origin of the script — the origin is defined by your web
page, not by what else it includes. It works only because the server wraps JSON in
a calback function, which should be defined in your code, and will be executed by
&amp;lt;script&amp;gt; when loaded. Most famous example of JSONP would be Yahoo services,
including Flickr.
&lt;/p&gt;
&lt;p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"&gt;
Another technique is to use window.name to transfer the information.&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://www.sitepen.com/blog/2008/07/22/windowname-transport/" rel="nofollow" style="border-width: 0px; margin: 0px; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(0, 119, 204); text-decoration: none; cursor: pointer;"&gt;This
technique was detailed by Kris Zyp four month ago&lt;/a&gt;. Additionally his article compares
window.name transport with JSONP. I don't know any high-profile service provider that
supports this new transport. Obviously it will change over time.
&lt;/p&gt;
&lt;p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"&gt;
Of course, I should mention the upcoming&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/cc288060%28VS.85%29.aspx" rel="nofollow" style="border-width: 0px; margin: 0px; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(0, 119, 204); text-decoration: none; cursor: pointer;"&gt;Microsoft
XDomainRequest&lt;/a&gt;. It is being planned to be shipped with IE8, and no other vendors
committed to support it, but it was presented for the inclusion in HTML 5. XDR is
a useful piece of functionality, but I suspect it'll be changed several times before
being accepted.
&lt;/p&gt;
&lt;p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"&gt;
If you looked in the links you probably know by now that all these methods require
a certain level of cooperation from a 3rd-party server. You cannot use random services
at will. If you do have to use an uncooperative service, the only solution is to proxy
it through your own server with all associated problems: the questionable legality,
the reduced performance, the increased load on your server, the reduced number of
connections between user's browser and your server, and so on.
&lt;/p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="border-width: 0px; margin: 0px 0px 1em; padding: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; clear: both;"&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=be22c897-92c4-4869-9cb5-71bc1ed75667" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,be22c897-92c4-4869-9cb5-71bc1ed75667.aspx</comments>
      <category>Javascript</category>
      <category>Web Design</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=83900cde-c091-4842-ad20-b965dcf348d6</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,83900cde-c091-4842-ad20-b965dcf348d6.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,83900cde-c091-4842-ad20-b965dcf348d6.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=83900cde-c091-4842-ad20-b965dcf348d6</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">If we need to set an ASP.NET Literal with
escaped HTML encoded via javascript's escape() method, we need to use Server.UrlDecode
rather than Server.HtmlDecode.<br /><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=83900cde-c091-4842-ad20-b965dcf348d6" /></body>
      <title>Using javascript escape and ASP.NET Literal controls</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,83900cde-c091-4842-ad20-b965dcf348d6.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/12/17/UsingJavascriptEscapeAndASPNETLiteralControls.aspx</link>
      <pubDate>Thu, 17 Dec 2009 16:14:48 GMT</pubDate>
      <description>If we need to set an ASP.NET Literal with escaped HTML encoded via javascript's escape() method, we need to use Server.UrlDecode rather than Server.HtmlDecode.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=83900cde-c091-4842-ad20-b965dcf348d6" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,83900cde-c091-4842-ad20-b965dcf348d6.aspx</comments>
      <category>ASP.NET</category>
      <category>Javascript</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=de37a2a0-c211-4de3-bb81-5976807691b5</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,de37a2a0-c211-4de3-bb81-5976807691b5.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,de37a2a0-c211-4de3-bb81-5976807691b5.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=de37a2a0-c211-4de3-bb81-5976807691b5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">As more and more code goes client-side,
it's important to make sure that proper SE principles are still applied.<br /><br />
I'm still using Notepad2 but have to seriously start looking at more advanced(!) IDEs.<br /><br />
At the moment though, I'm happy to make do with stand-alone tools for tasks such as
documentation or minimization etc.<br /><br />
For documentation, I've used jsdoc toolkit which you can find here - <a href="http://code.google.com/p/jsdoc-toolkit/">http://code.google.com/p/jsdoc-toolkit/</a><br /><br />
To confuse things, the documentation for this documentation-generator is found at
the project's previous incarnation - <a href="http://jsdoc.sourceforge.net/">http://jsdoc.sourceforge.net/</a><br /><br />
The FAQ on how to run it etc is here - <a href="http://code.google.com/p/jsdoc-toolkit/wiki/FAQ">http://code.google.com/p/jsdoc-toolkit/wiki/FAQ</a><br /><br />
Run it using the following command - 
<br /><br />
java -jar jsrun.jar app\run.js -a -t=templates\jsdoc mycode.js<br /><br /><br /><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=de37a2a0-c211-4de3-bb81-5976807691b5" /></body>
      <title>Javascript documentation</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,de37a2a0-c211-4de3-bb81-5976807691b5.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/11/18/JavascriptDocumentation.aspx</link>
      <pubDate>Wed, 18 Nov 2009 18:45:52 GMT</pubDate>
      <description>As more and more code goes client-side, it's important to make sure that proper SE principles are still applied.&lt;br&gt;
&lt;br&gt;
I'm still using Notepad2 but have to seriously start looking at more advanced(!) IDEs.&lt;br&gt;
&lt;br&gt;
At the moment though, I'm happy to make do with stand-alone tools for tasks such as
documentation or minimization etc.&lt;br&gt;
&lt;br&gt;
For documentation, I've used jsdoc toolkit which you can find here - &lt;a href="http://code.google.com/p/jsdoc-toolkit/"&gt;http://code.google.com/p/jsdoc-toolkit/&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
To confuse things, the documentation for this documentation-generator is found at
the project's previous incarnation - &lt;a href="http://jsdoc.sourceforge.net/"&gt;http://jsdoc.sourceforge.net/&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
The FAQ on how to run it etc is here - &lt;a href="http://code.google.com/p/jsdoc-toolkit/wiki/FAQ"&gt;http://code.google.com/p/jsdoc-toolkit/wiki/FAQ&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
Run it using the following command - 
&lt;br&gt;
&lt;br&gt;
java -jar jsrun.jar app\run.js -a -t=templates\jsdoc mycode.js&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=de37a2a0-c211-4de3-bb81-5976807691b5" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,de37a2a0-c211-4de3-bb81-5976807691b5.aspx</comments>
      <category>Javascript</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=5f3d8242-8942-4232-be2d-c5ac4a1a4b6e</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,5f3d8242-8942-4232-be2d-c5ac4a1a4b6e.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,5f3d8242-8942-4232-be2d-c5ac4a1a4b6e.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=5f3d8242-8942-4232-be2d-c5ac4a1a4b6e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Tried having a bash with <a href="http://www.jsunit.net/">jsUnit </a>today,
and got instantly stuck when trying to run a basic test case.<br /><br />
FireFox simply hung, and I really had no idea why.  For the first time, IE8 worked
fine!<br /><br />
Thanks to <a href="http://digitalmihailo.blogspot.com/2008/06/make-jsunit-work-in-firefox-30.html">this
post</a>, it was straightforward enough to get working.<br /><br />
First you have to modify the jsUnitTestManager.js file, and replace/insert the following
code:<br /><br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">function</span> isFF3()
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">return</span> (
navigator.userAgent.<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">toLowerCase</span>().<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">indexOf</span>(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"iceweasel/3"</span>)
!<span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span> -1
|| navigator.userAgent.<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">toLowerCase</span>().<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">indexOf</span>(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"firefox/3"</span>)
!<span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span> -1
) } <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">function</span> browserSupportsReadingFullPathFromFileField()
{ <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">return</span> !isOpera()
&amp;&amp; !isIE7() &amp;&amp; !isFF3(); <span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">//return
!isOpera() &amp;&amp; !isIE7();</span> }</span></pre><br />
Secondly, you'll have to disable the draconian security that new FF introduces. 
Type about:config in your address bar and then set <b>security.fileuri.strict_origin_policy
to false</b>.  This will allow FireFox to access local resources without a problem.<br /><br />
Now it will have disabled the browse to field, so the only way you can run your tests
is directly via the address bar, as so:<br /><br /><b>&lt;path to testrunner.html&gt;?testpage=&lt;path to mytests.html&gt;</b><br /><br /><br /><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=5f3d8242-8942-4232-be2d-c5ac4a1a4b6e" /></body>
      <title>Using jsUnit in FireFox 3</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,5f3d8242-8942-4232-be2d-c5ac4a1a4b6e.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/09/23/UsingJsUnitInFireFox3.aspx</link>
      <pubDate>Wed, 23 Sep 2009 09:58:52 GMT</pubDate>
      <description>Tried having a bash with &lt;a href="http://www.jsunit.net/"&gt;jsUnit &lt;/a&gt;today, and got
instantly stuck when trying to run a basic test case.&lt;br&gt;
&lt;br&gt;
FireFox simply hung, and I really had no idea why.&amp;nbsp; For the first time, IE8 worked
fine!&lt;br&gt;
&lt;br&gt;
Thanks to &lt;a href="http://digitalmihailo.blogspot.com/2008/06/make-jsunit-work-in-firefox-30.html"&gt;this
post&lt;/a&gt;, it was straightforward enough to get working.&lt;br&gt;
&lt;br&gt;
First you have to modify the jsUnitTestManager.js file, and replace/insert the following
code:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;function&lt;/span&gt; isFF3()
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; (
navigator.userAgent.&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;toLowerCase&lt;/span&gt;().&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;indexOf&lt;/span&gt;(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"iceweasel/3"&lt;/span&gt;)
!&lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; -1
|| navigator.userAgent.&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;toLowerCase&lt;/span&gt;().&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;indexOf&lt;/span&gt;(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"firefox/3"&lt;/span&gt;)
!&lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; -1
) } &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;function&lt;/span&gt; browserSupportsReadingFullPathFromFileField()
{ &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; !isOpera()
&amp;amp;&amp;amp; !isIE7() &amp;amp;&amp;amp; !isFF3(); &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;//return
!isOpera() &amp;amp;&amp;amp; !isIE7();&lt;/span&gt; }&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
Secondly, you'll have to disable the draconian security that new FF introduces.&amp;nbsp;
Type about:config in your address bar and then set &lt;b&gt;security.fileuri.strict_origin_policy
to false&lt;/b&gt;.&amp;nbsp; This will allow FireFox to access local resources without a problem.&lt;br&gt;
&lt;br&gt;
Now it will have disabled the browse to field, so the only way you can run your tests
is directly via the address bar, as so:&lt;br&gt;
&lt;br&gt;
&lt;b&gt;&amp;lt;path to testrunner.html&amp;gt;?testpage=&amp;lt;path to mytests.html&amp;gt;&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=5f3d8242-8942-4232-be2d-c5ac4a1a4b6e" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,5f3d8242-8942-4232-be2d-c5ac4a1a4b6e.aspx</comments>
      <category>Javascript</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=527b7bb9-7896-4c8c-abaf-c07f283f619b</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,527b7bb9-7896-4c8c-abaf-c07f283f619b.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,527b7bb9-7896-4c8c-abaf-c07f283f619b.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=527b7bb9-7896-4c8c-abaf-c07f283f619b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Looking at in-line validation for a new
website, came across this cracking jQuery component - <a href="http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/">http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/</a><br /><br />
Couple of gotchas to remember with this:<br /><br /><ul><li>
It expects the validation class to be FIRST.  So you'll need to remove existing
classes and then add them back again at the end.</li><li>
It has a bug whereby when you resize/move the window the error divs draw in a new
position.  I've fixed this by modifying the buildPrompt() function as below:</li></ul><br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">var</span> divErrorId <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span> 'divError_' <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">+</span> linkTofield;
        $('#' <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">+</span> divErrorId).remove();
                 
        $(divFormError).attr('id', divErrorId);</span></pre><br />
This very simply identifies each error div according to the field it is displaying
against, and removes any existing ones before it adds a new one.<br /><br />
Best practice would be to do this automatically rather than in-line, so you may want
to have a function like so:<br /><br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">function</span> registerValidation(id,
validation) { <span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;">//validation
has to be the first class declared</span><span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">var</span> elementClassList <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">=</span> $(id).attr('class');
$(id).removeClass(elementClassList); $(id).addClass(validation); $(id).addClass(elementClassList);
}</span></pre>
call this from doc.ready in your page as so<br /><br /><pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;">$(document).ready(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;">function</span>()
{ registerValidation(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"#txtMyField"</span>,
'validate[required,custom[onlyLetter],<span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;">length</span>[0,100]]'); </span></pre><br /><br /><br /><br />
I haven't tested the above code to any great degree yet, leave a comment if you spot
any goofs!<br /><br />
Also, there appears to be another bug if you want to call the javascript directly,
so you need to make sure you set the settings like so:<br /><br /><dl class="codebox"><dd><code>  var settings = jQuery.extend({<br />
      allrules:allRules,          
    <br />
      inlineValidation: true,   <br />
      ajaxSubmit: false,<br />
      promptPosition: "topRight",   // OPENNING BOX
POSITION, IMPLEMENTED: topLeft, topRight, bottomLeft, centerRight, bottomRight<br />
      success : false,<br />
      failure : function() {}<br />
   }, settings);<br /><br />
$.validationEngine.loadValidation("#txtAddressLine1", settings);  
<br /><br /></code></dd></dl><br /><br />
Then you want to check 
<br /><br /><dl class="codebox"><dt>Code: <a href="http://www.position-absolute.com/forum/viewtopic.php?f=4&amp;t=11#" onclick="selectCode(this); return false;">Select
all</a></dt><dd><code>$.validationEngine.isError</code></dd></dl><br /><br />
for an error.<br /><br /><br /><br /><br /><br /><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=527b7bb9-7896-4c8c-abaf-c07f283f619b" /></body>
      <title>jQuery inline validation</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,527b7bb9-7896-4c8c-abaf-c07f283f619b.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/09/22/jQueryInlineValidation.aspx</link>
      <pubDate>Tue, 22 Sep 2009 13:02:53 GMT</pubDate>
      <description>Looking at in-line validation for a new website, came across this cracking jQuery component - &lt;a href="http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/"&gt;http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
Couple of gotchas to remember with this:&lt;br&gt;
&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;
It expects the validation class to be FIRST.&amp;nbsp; So you'll need to remove existing
classes and then add them back again at the end.&lt;/li&gt;
&lt;li&gt;
It has a bug whereby when you resize/move the window the error divs draw in a new
position.&amp;nbsp; I've fixed this by modifying the buildPrompt() function as below:&lt;/li&gt;
&lt;/ul&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;var&lt;/span&gt; divErrorId &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; 'divError_' &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;+&lt;/span&gt; linkTofield;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$('#' &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;+&lt;/span&gt; divErrorId).remove();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$(divFormError).attr('id', divErrorId);&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
This very simply identifies each error div according to the field it is displaying
against, and removes any existing ones before it adds a new one.&lt;br&gt;
&lt;br&gt;
Best practice would be to do this automatically rather than in-line, so you may want
to have a function like so:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;function&lt;/span&gt; registerValidation(id,
validation) { &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;//validation
has to be the first class declared&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;var&lt;/span&gt; elementClassList &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; $(id).attr('class');
$(id).removeClass(elementClassList); $(id).addClass(validation); $(id).addClass(elementClassList);
}&lt;/span&gt;&lt;/pre&gt;
call this from doc.ready in your page as so&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;$(document).ready(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;function&lt;/span&gt;()
{ registerValidation(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"#txtMyField"&lt;/span&gt;,
'validate[required,custom[onlyLetter],&lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px;"&gt;length&lt;/span&gt;[0,100]]'); &lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I haven't tested the above code to any great degree yet, leave a comment if you spot
any goofs!&lt;br&gt;
&lt;br&gt;
Also, there appears to be another bug if you want to call the javascript directly,
so you need to make sure you set the settings like so:&lt;br&gt;
&lt;br&gt;
&lt;dl class="codebox"&gt;
&lt;dd&gt;&lt;code&gt;&amp;nbsp; var settings = jQuery.extend({&lt;br&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;allrules:allRules,&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;inlineValidation: true,&amp;nbsp; &amp;nbsp;&lt;br&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;ajaxSubmit: false,&lt;br&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;promptPosition: "topRight",&amp;nbsp; &amp;nbsp;// OPENNING BOX
POSITION, IMPLEMENTED: topLeft, topRight, bottomLeft, centerRight, bottomRight&lt;br&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;success : false,&lt;br&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;failure : function() {}&lt;br&gt;
&amp;nbsp; &amp;nbsp;}, settings);&lt;br&gt;
&lt;br&gt;
$.validationEngine.loadValidation("#txtAddressLine1", settings);&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
&lt;/code&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;br&gt;
&lt;br&gt;
Then you want to check 
&lt;br&gt;
&lt;br&gt;
&lt;dl class="codebox"&gt;
&lt;dt&gt;Code: &lt;a href="http://www.position-absolute.com/forum/viewtopic.php?f=4&amp;amp;t=11#" onclick="selectCode(this); return false;"&gt;Select
all&lt;/a&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;$.validationEngine.isError&lt;/code&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;br&gt;
&lt;br&gt;
for an error.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=527b7bb9-7896-4c8c-abaf-c07f283f619b" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,527b7bb9-7896-4c8c-abaf-c07f283f619b.aspx</comments>
      <category>Javascript</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=10bbed4e-b160-41ab-81f7-b8615e2124b6</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,10bbed4e-b160-41ab-81f7-b8615e2124b6.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,10bbed4e-b160-41ab-81f7-b8615e2124b6.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=10bbed4e-b160-41ab-81f7-b8615e2124b6</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Javascript, as I'm sure you are aware,
has no in-built knowledge of classes, inheritance or interfaces as other OO programming
languages do.  We have to engineer these using various constructs and work-arounds,
in order that we maintain code order and good programming principles.  With more
and more code on the client-side in order to provide rich user experiences, this is
vital or you'll be condemned to snippet hell.  
<br /><br /><font size="4">Simple View-Controller pattern</font><br /><br />
Say you want to implement a bog-standard separation of View-Controller logic. 
The View is a collection of controls, and directly handles the interaction of these
controls before packaging them up and sending them on to the Controller.  In
the Windows world, we would raise a custom event from the View which the Controller
hooks into, but given Javascript's flexible duck-typing, we can bind the View directly
to the Controller and inject a Stub in the future for testing if we should wish.<br /><br />
Anyway, our View has a button.  When the user clicks the button, we want the
Controller to know what to do, not the View.  
<br /><br />
The View will have a SetController method, which when set, will bind the button's
click event (I've omitted the unbind to clear things up) to the appropriate method
on the Controller.<br /><br />
Like so:<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">function</span> RefineSearchView()
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">var</span> _controller;
} RefineSearchView.<span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">prototype</span>.setRefineSearchController <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">function</span>(newController)
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>._controller <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> newController;
$(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"#btnStreetRefineFilter"</span>).bind(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"click"</span>,
createMethodReference(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>._controller, <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"HandleFilter"</span>));
}</span></pre><br />
NOTE: this is where it gets interesting.  createMethodReference() - what's that
all about?  Why don't I just do:<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">$(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"#btnStreetRefineFilter"</span>).bind(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"click"</span>, <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>._controller.<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">HandleFilter</span>); </span></pre><br />
You could expect this to simply bind the click event on to this particular View's
Controller's HandleFilter() method.  Non?<br /><br />
Non.  The reason this won't work is because when we bind the click event, we
are not in the correct context.  This article - with the suitably majestic title
of <a href="http://www.brockman.se/writing/method-references.html.utf8">Object-Oriented
Event Listening through Partial Application in JavaScript</a> - will explain it in
ways far better than I could!<br /><br />
This is to demonstrate in it's most basic format - we can go on as the article suggests,
wave our magic refactoring wand until it transforms from the ugly duckling of createMethodReference
into the elegant swan of 
<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.element.onclick <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.buttonClicked.bind(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>);</span></pre><br /><br /><br /><font size="4">Controller Handle method</font><br /><br />
Now we have this set up, it's just a case of implementing our HandleFilter method
on the controller:<br /><br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">RefineSearchController.<span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">prototype</span>.HandleFilter <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">function</span>(e)
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">var</span> searchParams <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>._searchController.getSearchParams(); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">var</span> locality <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>._view.getLocality(); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">var</span> town <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>._view.getTown();
searchParams._locality <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> locality;
searchParams._town <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> town; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>._searchController.setSearchParams(searchParams); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>._searchController.<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">search</span>(); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>._view.updateFilterStatus(searchParams);
}</span></pre><br />
So now we have completely separated our code into View and Logic sections.  This
example was a bit contrived, but you can imagine the situation where our View has
to gather information from a number of controls before wrapping these up and sending
them on to the Controller in a nice abstract package, similar to the searchParams
outlined above.<br /><br />
Not only does this approach give benefits in terms of maintenance, but the stage is
now set for unit-testing and programmatically driving your dynamic web page...<br /><p></p><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=10bbed4e-b160-41ab-81f7-b8615e2124b6" /></body>
      <title>Object-oriented Event Handling in Javascript</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,10bbed4e-b160-41ab-81f7-b8615e2124b6.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/05/26/ObjectorientedEventHandlingInJavascript.aspx</link>
      <pubDate>Tue, 26 May 2009 22:04:57 GMT</pubDate>
      <description>Javascript, as I'm sure you are aware, has no in-built knowledge of classes, inheritance or interfaces as other OO programming languages do.&amp;nbsp; We have to engineer these using various constructs and work-arounds, in order that we maintain code order and good programming principles.&amp;nbsp; With more and more code on the client-side in order to provide rich user experiences, this is vital or you'll be condemned to snippet hell.&amp;nbsp; &lt;br&gt;
&lt;br&gt;
&lt;font size="4"&gt;Simple View-Controller pattern&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
Say you want to implement a bog-standard separation of View-Controller logic.&amp;nbsp;
The View is a collection of controls, and directly handles the interaction of these
controls before packaging them up and sending them on to the Controller.&amp;nbsp; In
the Windows world, we would raise a custom event from the View which the Controller
hooks into, but given Javascript's flexible duck-typing, we can bind the View directly
to the Controller and inject a Stub in the future for testing if we should wish.&lt;br&gt;
&lt;br&gt;
Anyway, our View has a button.&amp;nbsp; When the user clicks the button, we want the
Controller to know what to do, not the View.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
The View will have a SetController method, which when set, will bind the button's
click event (I've omitted the unbind to clear things up) to the appropriate method
on the Controller.&lt;br&gt;
&lt;br&gt;
Like so:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;function&lt;/span&gt; RefineSearchView()
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;var&lt;/span&gt; _controller;
} RefineSearchView.&lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;prototype&lt;/span&gt;.setRefineSearchController &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;function&lt;/span&gt;(newController)
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;._controller &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; newController;
$(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"#btnStreetRefineFilter"&lt;/span&gt;).bind(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"click"&lt;/span&gt;,
createMethodReference(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;._controller, &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"HandleFilter"&lt;/span&gt;));
}&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
NOTE: this is where it gets interesting.&amp;nbsp; createMethodReference() - what's that
all about?&amp;nbsp; Why don't I just do:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;$(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"#btnStreetRefineFilter"&lt;/span&gt;).bind(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"click"&lt;/span&gt;, &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;._controller.&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;HandleFilter&lt;/span&gt;); &lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
You could expect this to simply bind the click event on to this particular View's
Controller's HandleFilter() method.&amp;nbsp; Non?&lt;br&gt;
&lt;br&gt;
Non.&amp;nbsp; The reason this won't work is because when we bind the click event, we
are not in the correct context.&amp;nbsp; This article - with the suitably majestic title
of &lt;a href="http://www.brockman.se/writing/method-references.html.utf8"&gt;Object-Oriented
Event Listening through Partial Application in JavaScript&lt;/a&gt; - will explain it in
ways far better than I could!&lt;br&gt;
&lt;br&gt;
This is to demonstrate in it's most basic format - we can go on as the article suggests,
wave our magic refactoring wand until it transforms from the ugly duckling of createMethodReference
into the elegant swan of 
&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.element.onclick &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.buttonClicked.bind(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;);&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;font size="4"&gt;Controller Handle method&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
Now we have this set up, it's just a case of implementing our HandleFilter method
on the controller:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;RefineSearchController.&lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;prototype&lt;/span&gt;.HandleFilter &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;function&lt;/span&gt;(e)
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;var&lt;/span&gt; searchParams &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;._searchController.getSearchParams(); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;var&lt;/span&gt; locality &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;._view.getLocality(); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;var&lt;/span&gt; town &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;._view.getTown();
searchParams._locality &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; locality;
searchParams._town &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; town; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;._searchController.setSearchParams(searchParams); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;._searchController.&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;search&lt;/span&gt;(); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;._view.updateFilterStatus(searchParams);
}&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
So now we have completely separated our code into View and Logic sections.&amp;nbsp; This
example was a bit contrived, but you can imagine the situation where our View has
to gather information from a number of controls before wrapping these up and sending
them on to the Controller in a nice abstract package, similar to the searchParams
outlined above.&lt;br&gt;
&lt;br&gt;
Not only does this approach give benefits in terms of maintenance, but the stage is
now set for unit-testing and programmatically driving your dynamic web page...&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=10bbed4e-b160-41ab-81f7-b8615e2124b6" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,10bbed4e-b160-41ab-81f7-b8615e2124b6.aspx</comments>
      <category>Javascript</category>
      <category>Patterns</category>
      <category>TDD</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=3f856e66-3580-418f-85d0-e2d788848e5d</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,3f856e66-3580-418f-85d0-e2d788848e5d.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,3f856e66-3580-418f-85d0-e2d788848e5d.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=3f856e66-3580-418f-85d0-e2d788848e5d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Okay, so this is hardly a ground-breaking
topic.  But I think it's a valid showcase as to how rich, UI experiences can
be delivered via the browser without costing the earth.<br /><br />
Picture the scene - I have a web page with a jquery Grid (<a href="http://www.flexigrid.info/">FlexiGrid</a> -
though I think I will be upgrading to <a href="http://www.trirand.com/blog/">jqGrid</a> due
to it's continued developer support - but more on this later) and a toolbar (a very
cool <a href="http://blog.itookia.com/post/How-to-create-VISTA-style-toolbar-with-CSS.aspx">Vista</a> style
toolbar).<br /><br />
One of the commands available on this toolbar is Print.  
<br /><br />
When the user clicks this command, a jquery UI dialog pops up, displaying a selection
of valid reports which the user may select.<br /><br />
This reports list is generated from the server.<br /><br />
So, if all of this was to be done as the page loads - well, you can imagine the performance
would be unacceptable.  But I want to keep the user experience rich - I don't
want them navigating from the grid to another page when they click Print.  So
I need to do this using AJAX.<br /><br />
The process is as follows:<br /><br />
 <br /><p></p><img src="http://www.duncangunn.me.uk/dasblog/content/binary/PrintUIAjax.jpg" border="0" /><br /><br /><br />
The Print Button Handler calls the PrintWebService via an AJAX call using some jquery
magic as so:<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"> $.ajax({
type: <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"POST"</span>,
contentType: <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"application/json;
charset=utf-8"</span>, data: <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"{}"</span>,
dataType: <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"json"</span>,
url: <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"http://localhost/PrintService/PrintService.asmx/RenderPrintDialog"</span>,
success: <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">function</span>(data)
{ $(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"#printdialoginner"</span>).html(data.d);
$(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"#printdialog"</span>).dialog(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"open"</span>);
}, error: <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">function</span>(xhr,
status, errorThrown) { alert(status); alert(errorThrown); } });</span></pre><br />
Inside the service, we can get our reports from the server (from the database) at
our leisure.  Ish.<br /><br />
Now comes the clever part.  To return the UI, all we're doing is churning back
some HTML, right?  But what if we want to be able to visually SEE what we are
going to return, at design-time?  
<br /><br />
Thanks to Scott Gu, we can get full VS designer support when creating our dynamic
UI - check it out - <a href="http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx">http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx</a><br /><br />
So we create our .ascx controls as normal, pass the path to the ViewManager, and let
it get it's hands dirty.  It also keeps a nice, clean MVC separation.<br /><br />
Using this trick, I've been able to deliver a seamless, rich UI without having noticeable
effects for the user.  Okay, so there is a slight delay when they bring up the
Print dialog for the first time (I know, I've not mentioned caching here but do I
need to?) but it's barely noticeable.<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=3f856e66-3580-418f-85d0-e2d788848e5d" /></body>
      <title>Adding UI to a page dynamically</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,3f856e66-3580-418f-85d0-e2d788848e5d.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/05/20/AddingUIToAPageDynamically.aspx</link>
      <pubDate>Wed, 20 May 2009 19:57:56 GMT</pubDate>
      <description>Okay, so this is hardly a ground-breaking topic.&amp;nbsp; But I think it's a valid showcase as to how rich, UI experiences can be delivered via the browser without costing the earth.&lt;br&gt;
&lt;br&gt;
Picture the scene - I have a web page with a jquery Grid (&lt;a href="http://www.flexigrid.info/"&gt;FlexiGrid&lt;/a&gt; -
though I think I will be upgrading to &lt;a href="http://www.trirand.com/blog/"&gt;jqGrid&lt;/a&gt; due
to it's continued developer support - but more on this later) and a toolbar (a very
cool &lt;a href="http://blog.itookia.com/post/How-to-create-VISTA-style-toolbar-with-CSS.aspx"&gt;Vista&lt;/a&gt; style
toolbar).&lt;br&gt;
&lt;br&gt;
One of the commands available on this toolbar is Print.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
When the user clicks this command, a jquery UI dialog pops up, displaying a selection
of valid reports which the user may select.&lt;br&gt;
&lt;br&gt;
This reports list is generated from the server.&lt;br&gt;
&lt;br&gt;
So, if all of this was to be done as the page loads - well, you can imagine the performance
would be unacceptable.&amp;nbsp; But I want to keep the user experience rich - I don't
want them navigating from the grid to another page when they click Print.&amp;nbsp; So
I need to do this using AJAX.&lt;br&gt;
&lt;br&gt;
The process is as follows:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img src="http://www.duncangunn.me.uk/dasblog/content/binary/PrintUIAjax.jpg" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The Print Button Handler calls the PrintWebService via an AJAX call using some jquery
magic as so:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt; $.ajax({
type: &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"POST"&lt;/span&gt;,
contentType: &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"application/json;
charset=utf-8"&lt;/span&gt;, data: &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"{}"&lt;/span&gt;,
dataType: &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"json"&lt;/span&gt;,
url: &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"http://localhost/PrintService/PrintService.asmx/RenderPrintDialog"&lt;/span&gt;,
success: &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;function&lt;/span&gt;(data)
{ $(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"#printdialoginner"&lt;/span&gt;).html(data.d);
$(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"#printdialog"&lt;/span&gt;).dialog(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"open"&lt;/span&gt;);
}, error: &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;function&lt;/span&gt;(xhr,
status, errorThrown) { alert(status); alert(errorThrown); } });&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
Inside the service, we can get our reports from the server (from the database) at
our leisure.&amp;nbsp; Ish.&lt;br&gt;
&lt;br&gt;
Now comes the clever part.&amp;nbsp; To return the UI, all we're doing is churning back
some HTML, right?&amp;nbsp; But what if we want to be able to visually SEE what we are
going to return, at design-time?&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
Thanks to Scott Gu, we can get full VS designer support when creating our dynamic
UI - check it out - &lt;a href="http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx"&gt;http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
So we create our .ascx controls as normal, pass the path to the ViewManager, and let
it get it's hands dirty.&amp;nbsp; It also keeps a nice, clean MVC separation.&lt;br&gt;
&lt;br&gt;
Using this trick, I've been able to deliver a seamless, rich UI without having noticeable
effects for the user.&amp;nbsp; Okay, so there is a slight delay when they bring up the
Print dialog for the first time (I know, I've not mentioned caching here but do I
need to?) but it's barely noticeable.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=3f856e66-3580-418f-85d0-e2d788848e5d" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,3f856e66-3580-418f-85d0-e2d788848e5d.aspx</comments>
      <category>Javascript</category>
      <category>Patterns</category>
      <category>Web Design</category>
    </item>
    <item>
      <trackback:ping>http://www.duncangunn.me.uk/dasblog/Trackback.aspx?guid=73f1a993-c97d-498b-818f-98b105c414da</trackback:ping>
      <pingback:server>http://www.duncangunn.me.uk/dasblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.duncangunn.me.uk/dasblog/PermaLink,guid,73f1a993-c97d-498b-818f-98b105c414da.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.duncangunn.me.uk/dasblog/CommentView,guid,73f1a993-c97d-498b-818f-98b105c414da.aspx</wfw:comment>
      <wfw:commentRss>http://www.duncangunn.me.uk/dasblog/SyndicationService.asmx/GetEntryCommentsRss?guid=73f1a993-c97d-498b-818f-98b105c414da</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">We had a review of some client-side code
today, as it had started to <a href="http://en.wikipedia.org/wiki/Code_smell">smell</a> a
little "off".<br /><br />
As always, the indication I use when quickly reviewing a design is - how easy is this
to unit test?<br /><br /><br /><b><font size="4">The problem</font></b><br /><br />
Logic all over the place.  Massive "if" statements.  More spaghetti than
an episode of the Sopranos.<br /><br />
For our web app, we have a number of "tools"; when the user clicks one, it puts the
application into that particular tool mode, which means that any subsequent mouse
events have to check which mode we are currently in, and take the appropriate action. 
Not only that, but we have introduced a vector view layer (SVG) so we have some tools
that update the image layer, some that update the vector layer, and some that update
both.<br /><br /><font size="4"><b>The solution</b></font><br /><br />
Stick to established patterns.  When dealing with UI, everything boils down to
MVC in the end.<br /><br /><br />
 <img src="content/binary/Class%20Diagram112.jpg" border="0" /><br /><br />
What we've done here is separate out each tool into an "Interactor" class.  The
View provides the abstraction of each view type, and also the UI events.  The
Controller provides the glue to stick everything together.<br /><br />
In Summary:<br /><br /><ul><li>
The View knows about the different view types, and knows about UI events.</li><li>
The View Types know how to render themselves, but are still pretty "dumb".<br /></li><li>
The Controller knows about UI events, and things called Interactors, but it doesn't
know what Interactors are or what they actually do.</li><li>
The Interactors know exactly what is going on, they interact directly with the view
type and the server.<br /></li></ul><br /><br /><img src="http://www.duncangunn.me.uk/dasblog/content/binary/Interactor.jpg" border="0" /><br /><br /><br />
This solution certainly isn't "pure" by any means - there is no M!  And I am
in no doubt that we could refine this further.  
<br /><br />
But the main thing is we have separated everything out into distinct, logical components
that have a defined and related set of responsibilities. 
<br /><br />
What's more this design is geared for unit testing.  We can test the Interactors
independently of the views (by dependency injection, which we will do later) and then
add another layer of tests integrating the Controller, and so on.<br /><br /><br /><br /><img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=73f1a993-c97d-498b-818f-98b105c414da" /></body>
      <title>Client-side MVC</title>
      <guid isPermaLink="false">http://www.duncangunn.me.uk/dasblog/PermaLink,guid,73f1a993-c97d-498b-818f-98b105c414da.aspx</guid>
      <link>http://www.duncangunn.me.uk/dasblog/2009/05/13/ClientsideMVC.aspx</link>
      <pubDate>Wed, 13 May 2009 20:16:28 GMT</pubDate>
      <description>We had a review of some client-side code today, as it had started to &lt;a href="http://en.wikipedia.org/wiki/Code_smell"&gt;smell&lt;/a&gt; a
little "off".&lt;br&gt;
&lt;br&gt;
As always, the indication I use when quickly reviewing a design is - how easy is this
to unit test?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;&lt;font size="4"&gt;The problem&lt;/font&gt;&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
Logic all over the place.&amp;nbsp; Massive "if" statements.&amp;nbsp; More spaghetti than
an episode of the Sopranos.&lt;br&gt;
&lt;br&gt;
For our web app, we have a number of "tools"; when the user clicks one, it puts the
application into that particular tool mode, which means that any subsequent mouse
events have to check which mode we are currently in, and take the appropriate action.&amp;nbsp;
Not only that, but we have introduced a vector view layer (SVG) so we have some tools
that update the image layer, some that update the vector layer, and some that update
both.&lt;br&gt;
&lt;br&gt;
&lt;font size="4"&gt;&lt;b&gt;The solution&lt;/b&gt;&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
Stick to established patterns.&amp;nbsp; When dealing with UI, everything boils down to
MVC in the end.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&lt;img src="content/binary/Class%20Diagram112.jpg" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
What we've done here is separate out each tool into an "Interactor" class.&amp;nbsp; The
View provides the abstraction of each view type, and also the UI events.&amp;nbsp; The
Controller provides the glue to stick everything together.&lt;br&gt;
&lt;br&gt;
In Summary:&lt;br&gt;
&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;
The View knows about the different view types, and knows about UI events.&lt;/li&gt;
&lt;li&gt;
The View Types know how to render themselves, but are still pretty "dumb".&lt;br&gt;
&lt;/li&gt;
&lt;li&gt;
The Controller knows about UI events, and things called Interactors, but it doesn't
know what Interactors are or what they actually do.&lt;/li&gt;
&lt;li&gt;
The Interactors know exactly what is going on, they interact directly with the view
type and the server.&lt;br&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;br&gt;
&lt;br&gt;
&lt;img src="http://www.duncangunn.me.uk/dasblog/content/binary/Interactor.jpg" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
This solution certainly isn't "pure" by any means - there is no M!&amp;nbsp; And I am
in no doubt that we could refine this further.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
But the main thing is we have separated everything out into distinct, logical components
that have a defined and related set of responsibilities. 
&lt;br&gt;
&lt;br&gt;
What's more this design is geared for unit testing.&amp;nbsp; We can test the Interactors
independently of the views (by dependency injection, which we will do later) and then
add another layer of tests integrating the Controller, and so on.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;img width="0" height="0" src="http://www.duncangunn.me.uk/dasblog/aggbug.ashx?id=73f1a993-c97d-498b-818f-98b105c414da" /&gt;</description>
      <comments>http://www.duncangunn.me.uk/dasblog/CommentView,guid,73f1a993-c97d-498b-818f-98b105c414da.aspx</comments>
      <category>Patterns</category>
      <category>TDD</category>
      <category>Javascript</category>
    </item>
  </channel>
</rss>