<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nicholascloud.com &#187; jQuery</title>
	<atom:link href="http://www.nicholascloud.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nicholascloud.com</link>
	<description>Encapsulate what varies.</description>
	<lastBuildDate>Fri, 03 Feb 2012 17:19:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>IE does something better than Firefox, hell freezes over</title>
		<link>http://www.nicholascloud.com/2010/02/ie-does-something-better-than-firefox-hell-freezes-over/</link>
		<comments>http://www.nicholascloud.com/2010/02/ie-does-something-better-than-firefox-hell-freezes-over/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 23:59:45 +0000</pubDate>
		<dc:creator>ncloud</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[internet explorer]]></category>

		<guid isPermaLink="false">http://www.nicholascloud.com/?p=171</guid>
		<description><![CDATA[Like most web developers, I write markup and Javascript that will work on both IE and Firefox, and if I&#8217;m particularly ambitious, Chome and Safari.  I&#8217;ve been writing a lot of Javascript lately, which has been a good learning experience but frustrating at the same time.  I don&#8217;t have the luxury of using jQuery on [...]]]></description>
			<content:encoded><![CDATA[<p>Like most web developers, I write markup and Javascript that will work on both IE and Firefox, and if I&#8217;m particularly ambitious, Chome and Safari.  I&#8217;ve been writing a lot of Javascript lately, which has been a good learning experience but frustrating at the same time.  I don&#8217;t have the luxury of using jQuery on my current project &#8212; I am pretty much restricted to Prototype for legacy reasons.  I was in the throes of reconciling a piece of code that copied values from one set of input fields to another, then accessing the innerHTML property of the target fields&#8217; containing div.  I was doing simple value assignments ($(&#8220;textBox1&#8243;).value = $(&#8220;textBox2&#8243;).value), and in IE everything worked like a peach.  Then I tested in Firefox and, to my amazement, the target input fields remained blank, even though FireBug assured me that the values were in fact being copied.  I was certain I was making a mistake somewhere in code, and spent an hour or so meticulously pouring over every line of Javascript, attempting to find the roadblock.  In desperation, I turned to the Google in search of an answer.  <a title="jQuery html() in Firefox (uses .innerHTML) ignores DOM changes" href="http://stackoverflow.com/questions/1388893/jquery-html-in-firefox-uses-innerhtml-ignores-dom-changes" target="_blank">What I found</a> surprised me enough that I felt compelled to write this post.</p>
<p><em>Firefox does not reflect dynamic DOM changes in the innerHTML property of a given DOM element.<br />
</em></p>
<p>This means that if you set the value of a text box to &#8220;123&#8243;, then call the innerHTML property of the DOM element that contains the text box, the value of the text box according to innerHTML will appear to be whatever it was prior to your alteration.  In practice, when the page posts back to the server, the value will actually be &#8220;123&#8243; and not what innerHTML reports, but the fact that Firefox ignores the change gives me yet one more gray hair.</p>
<p>To get around this glaring oversight, I had to use the setAttribute() method on the DOM input element:</p>
<p>$(&#8220;textBox1&#8243;).setAttribute(&#8220;value&#8221;, $(&#8220;textBox2&#8243;).value);</p>
<p>This accomplishes both tasks &#8212; updating the actual input value and changing the innerHTML property accordingly.</p>
<p>You&#8217;ve won this round IE&#8230; you&#8217;ve won this round.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nicholascloud.com/2010/02/ie-does-something-better-than-firefox-hell-freezes-over/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The cake is not a lie with MVC and JSON</title>
		<link>http://www.nicholascloud.com/2010/01/the-cake-is-not-a-lie-with-mvc-and-json/</link>
		<comments>http://www.nicholascloud.com/2010/01/the-cake-is-not-a-lie-with-mvc-and-json/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 19:39:53 +0000</pubDate>
		<dc:creator>ncloud</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.nicholascloud.com/?p=124</guid>
		<description><![CDATA[Ever since Google Maps showed us that rich internet applications were not only possible but pretty damn cool, developers have been striving to streamline the process of communicating with web application servers without refreshing the browser.  Initially this was done by writing JavaScript to work directly with the XMLHttpRequest object; but this was tedious and [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since Google Maps showed us that rich internet applications were not only possible but pretty damn cool, developers have been striving to streamline the process of communicating with web application servers without refreshing the browser.  Initially this was done by writing JavaScript to work directly with the XMLHttpRequest object; but this was tedious and not at all elegant.  Then some smart folk came up with the <a title="Prototype" href="http://www.prototypejs.org/" target="_blank">Prototype</a> JavaScript framework and made everyone&#8217;s lives easier by abstracting AJAX functionality, making it easier to work with. <a title="jQuery" href="http://jquery.com/" target="_blank"> jQuery</a> dazzled developers by simplifying the process even more.  XML began leaving a bad taste in everyone&#8217;s mouths, so other smart people figured out that instead of passing tagged markup data back and forth, these JavaScript frameworks could transmit and receive <a title="JSON" href="http://www.json.org/" target="_blank">JSON</a>.  Because JSON is the native way to serialize JavaScript objects, and because it is significantly less bloated than XML, it was the natural choice for future rich internet applications.</p>
<p>Now, I have a real passion for simplicity and elegance, so any web application framework that makes AJAX easier to write and maintain is my friend.  Since I&#8217;ve been learning ASP.NET MVC, I decided to see how it stood up to the challenge.  Like most everything else in MVC, AJAX/JSON support is <a title="The Cake is a Lie" href="http://www.urbandictionary.com/define.php?term=the+cake+is+a+lie" target="_blank"><em>cake</em>, not a <em>lie</em></a>.  A JSON driven AJAX call in MVC consists of:</p>
<ol>
<li>the page that makes the AJAX call, which sends and/or receives data;</li>
<li>a custom MVC action on a new or existing MVC controller that will handle the AJAX call, do the heavy data lifting, and return a result; and</li>
<li>a custom MVC Route that maps to the controller/action in Global.asax</li>
</ol>
<p>It&#8217;s easiest to start with the controller/action, so here we go.</p>
<p><a href="http://www.nicholascloud.com/wp-content/uploads/2010/01/json-controller-action.png"><img class="alignnone size-full wp-image-134" title="JSON Controller Action" src="http://www.nicholascloud.com/wp-content/uploads/2010/01/json-controller-action.png" alt="" width="502" height="241" /></a></p>
<p>There are a few things to note here, namely the return type of the Authorize() method (JsonResult instead of ActionResult), and the method called to render the data (Json() instead of View()).  Also, while any pre-defined, strongly typed object could be passed into the Json() method, an anonymous object works marvelously for simple values.  (As an aside, best practice dictates that the user repository be retrieved from an IoC container, but for the purpose of this demonstration and for space considerations, I have hard-coded it here.)  The logic in the action is very simple; we fetch a user based on credentials and return a value indicating if the user exists and if they have proper login access.  Because the action here is a simple method in a controller class, it is also possible to write automated unit tests to make sure that the method behaves as expected under a variety of conditions.</p>
<p>The Route for this controller/action is likewise very simple.</p>
<p><a href="http://www.nicholascloud.com/wp-content/uploads/2010/01/json-mvc-route.png"><img class="alignnone size-full wp-image-135" title="JSON MVC Route" src="http://www.nicholascloud.com/wp-content/uploads/2010/01/json-mvc-route.png" alt="" width="412" height="80" /></a></p>
<p>The route does not define any parameters for username or password.  This is because jQuery will append arguments to the route using the normal query string notation.  I suspect, but have not verified, that if a route was specified with parameters such as /{username}/{password}, and a dynamic URL was constructed in JavaScript and no arguments were passed to it, the functionality would be identical.</p>
<p>The actual view contains the most involved code.</p>
<p><a href="http://www.nicholascloud.com/wp-content/uploads/2010/01/json-html-markup.jpg"><img class="alignnone size-full wp-image-132" title="JSON HTML Markup" src="http://www.nicholascloud.com/wp-content/uploads/2010/01/json-html-markup.jpg" alt="" width="407" height="463" /></a></p>
<p>The form at the bottom contains two fields, Username and Password, and a simple input button that is used for submission.  The click event of the login button calls jQuery&#8217;s built in <a title="getJSON()" href="http://docs.jquery.com/Ajax/jQuery.getJSON" target="_blank">getJSON()</a> method, which takes the following arguments:</p>
<ol>
<li>the URL that will be used to submit the AJAX request (the URL defined in the MVC routing table);</li>
<li>an anonymous JavaScript object with properties identical to the method arguments of the action I am calling; and</li>
<li>a callback method that takes the return result of the AJAX request as an argument.  This result will have identical properties to the object we returned in our action method.</li>
</ol>
<p>Based on the return value from Authorize(), I show a popup dialog indicating whether the user should be allowed to log in.  (Best practices dictate that <em>authorization</em> and <em>authentication</em> be performed by two distinct services, but I have consolidated them here for brevity.)  Because AJAX calls are asynchronous, it is important to remember that the click() event will likely exit <em>before</em> the getJSON() function has completed.  If you add an hourglass or twirling Web 2.0 spiral (probably not the technical name), you will want to display it before getJSON() is called and hide it in the callback function instead of at the end of the click() event to ensure that it is displayed for the duration of the request.</p>
<p>That&#8217;s all for now.  I will post more on MVC later, but  I think we can all agree that:</p>
<blockquote><p><em>This was a triumph.<br />
I&#8217;m making a note here: HUGE SUCCESS.<br />
It&#8217;s hard to overstate my satisfaction.</em></p>
<p>&#8211;GlaDOS</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.nicholascloud.com/2010/01/the-cake-is-not-a-lie-with-mvc-and-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

