<?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>Kevin Grad &#187; jquery</title>
	<atom:link href="http://couchware.ca/www/kev/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://couchware.ca/www/kev</link>
	<description>Designer, Programmer and Co-Founder</description>
	<lastBuildDate>Fri, 30 Apr 2010 20:33:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introducing Arrayzing</title>
		<link>http://couchware.ca/www/kev/2009/02/07/introducing-arrayzing/</link>
		<comments>http://couchware.ca/www/kev/2009/02/07/introducing-arrayzing/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 22:36:47 +0000</pubDate>
		<dc:creator>kev</dc:creator>
				<category><![CDATA[API development]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[arrayzing]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[web app]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/kev/?p=96</guid>
		<description><![CDATA[I have been talking for a while now about how I would like to create an API of some sort. I want to start being a contributor to the open source community and develop some useful open source utilities. 

A natural spot for open source development is in the area of web development, an area [...]]]></description>
			<content:encoded><![CDATA[<p>I have been talking for a while now about how I would like to create an API of some sort. I want to start being a contributor to the open source community and develop some useful open source utilities. </p>
<p><span id="more-96"></span></p>
<p>A natural spot for open source development is in the area of web development, an area that I have recently taken a keen interest in. I currently work as a consultant to a company building clientside solutions for their servlet web app. I am also actively teaching myself Ruby and the Rails framework. If you would like to see an example of a site I am currently playing around with, hop on over to <a href="http://kgrad.selfip.org:3000">this site.</a></p>
<p>If you are going to write an API that helps with web development, you cannot go wrong with JavaScript. Every website uses JavaScript in some way or form. JavaScript is no longer an ugly semi-useful tool. It is a powerful and useful language that supports things like closures, named parameters and metaprogramming. </p>
<p>There are some amazing libraries being developed in the open source community for JavaScript. One that I have been using a lot lately and which I highly recommend is <a href="http://www.jquery.com">jQuery</a>. I talked a little bit about jQuery in my last post and gave a little bit of insight into it&#8217;s capabilities, but if you are a web programmer and you haven&#8217;t checked it out yet, I implore you to do so.</p>
<p>Anyway, Cam and I like to get into discussions about the things we would like to accomplish as programmers and often talk about programming language semantics, features and things we would like to add. We like to bounce ideas off of each other. One of our recent discussions had me suggesting the inclusion of some sort of container class that could be defined on the fly. This could be used as a way of returning multiple values from a function and reduce the need to wrap associations in simple classes. </p>
<p>Our most recent discussion however dealt with a problem Cam was working on in his own side-job where he had a collection of objects and wanted to filter them in some way using JavaScript. The easiest way we could think of to do this was to iterate through each object testing to see if it matched the criteria, if it did, throw it into another collection, otherwise go to the next object. This could look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">temp <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;hello&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;heLlo&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;jim&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;jack&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;hellO&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> re <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/hello/i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>temp.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>re<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
     result<span style="color: #009900;">&#91;</span>index<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> temp<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above code would return the result = ["hello", "heLlo", "hellO"];<br />
That is a lot of code to filter out an array. This spawned the creative process and got Cam working on a little public API that I am assisting with called <a href="http://couchware.ca/blogs/cam/arrayzing">Arrayzing</a>. You can find the source <a href="http://code.google.com/p/arrayzing/">here</a>.</p>
<p>In order to do the equivalent operation in arrayzing, the code would be:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">result <span style="color: #339933;">=</span> $a<span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span> <span style="color: #009966; font-style: italic;">/hello/i</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Pretty simple and beautifully powerful. Arrayzing uses jQuery like syntax to perform array manipulations and returns the wrapped set. Additionally, all Arrayzing functions can be chained together!</p>
<p>If you have a moment, please check it out and give us any ideas for features!</p>
]]></content:encoded>
			<wfw:commentRss>http://couchware.ca/www/kev/2009/02/07/introducing-arrayzing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using custom attributes for validation</title>
		<link>http://couchware.ca/www/kev/2009/02/02/using-custom-attributes-for-validation/</link>
		<comments>http://couchware.ca/www/kev/2009/02/02/using-custom-attributes-for-validation/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 18:20:39 +0000</pubDate>
		<dc:creator>kev</dc:creator>
				<category><![CDATA[jquery]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[selectors]]></category>
		<category><![CDATA[servlet]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/kev/?p=66</guid>
		<description><![CDATA[I was hired a couple of months ago to build a validation system for a legacy web-app built on Java Servlet technology. The app was a gigantic system with many existing web forms.

The job requirements included extendability, maintenance, minimal overhead, and it had to work with the current code, i.e. I couldn&#8217;t go and redesign [...]]]></description>
			<content:encoded><![CDATA[<p>I was hired a couple of months ago to build a validation system for a legacy web-app built on <a href="http://java.sun.com/products/servlet/">Java Servlet technology</a>. The app was a gigantic system with many existing web forms.</p>
<p><span id="more-66"></span></p>
<p>The job requirements included extendability, maintenance, minimal overhead, and it had to work with the current code, i.e. I couldn&#8217;t go and redesign the entire system. There were already hundreds of forms in the system with thousands of fields requiring validation. My first task was to explore the different possibilities. I came up with a few various schemes for both client side and server side validations. I opted to go with a client side solution for various reasons, including the fact that I could update the page with any error messages without ever needing to contact the server.</p>
<p>The major problem that I had to address was how to insert my validation functions within the already created web forms in an easy, extensible way. At the time I had been playing around with a JavaScript library known as <a href="http://jquery.com">jQuery</a>. I had very little experience with it, but it seemed very interesting. Cam had also recently begun using jQuery and was big on the library.</p>
<p>One of jQueries big features is easy manipulation of the DOM tree. jQuery ingeniously makes use of <a href="http://www.w3.org/TR/CSS2/selector.html">CSS Selectors</a> to wrap a group of elements in a jQuery wrapper. If you have never used CSS selectors, I HIGHLY recommend taking a look at them.</p>
<p>Using jQuery, the following simple code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.foo'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>returns an array of all DOM elements with the attribute class=&#8221;foo&#8221;. You can then iterate over this wrapped set in a number of ways. This is immensely powerful. Additionally, jQuery will recognize custom attributes.</p>
<p>The code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[required=true]'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>will return all input elements with a custom attribute required=&#8221;true&#8221;. What this means is, if you have in your HTML a text field like this:</p>
<p>&lt;input type=&#8221;text&#8221; required=&#8221;true&#8221;&gt;</p>
<p>The jQuery code above will find that element and return it in a wrapped jQuery array. I used this as the basis for my validation system.</p>
<p>What I did, was define a set of custom attributes, and a set of JavaScript functions based on requirements given to me by the hiring company. Then I created a validate() function which sequentially called all of my JavaScript functions. This validate function was meant to be called in an onSubmit() but could be called anytime.</p>
<p>If you ever wanted a field to be validated, you simply added my custom attributes to its HTML markup. So for example, if you wanted a field to be marked required, you simply went to that field and added required=&#8221;true&#8221;. If you wanted a field to be checked for a valid integer, you went to the field and added type=&#8221;integer&#8221;.</p>
<p>The beauty of this approach is that it allows a field to be marked with an arbitrary number of validations. And removing validation is as easy as removing the custom attribute. Additionally, if you add a new function to the set of JavaScipt functions, all you need to do to add it to a field is add the new custom attribute.</p>
<p>The way this works is, when the person submits the form, the validate function is called. The validate function uses jQuery to parse the DOM tree and iterate over the results. So within the validate function, the first function looked something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[required=yes]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>    
&nbsp;
  <span style="color: #006600; font-style: italic;">// validate the required strings.</span>
  <span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> validateRequired<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'field_name'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// If we had an error, concatenate it into the error string and add the errorCase</span>
  errorStr <span style="color: #339933;">+=</span> errorCheck<span style="color: #009900;">&#40;</span>str<span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What this did was, it searched the page for any input elements with the custom attribute required=&#8221;yes&#8221;. It then iterated over that set and passed the value into the JavaScript function validateRequired() which checked whether the field was non-empty. It then returned an error string from a function that properly formatted error messages.</p>
<p>Each method concatenated an error string when there were any errors, and at the end of the validate() function if there were any error messages it printed them to the screen in a custom div. Otherwise, it allowed the submit to go through.</p>
<p>This was done for every type of validation, even if there was no elements with that custom tag within the current page. This makes it easy to simply add new validations on the fly. Also, all the JavaScript functions are in a central place and are easy to modify/update and have it filter through the system automatically.</p>
<p>This solution worked perfectly as it was easy to implement and fit the requirements perfectly. The client was pleased and everything worked out.</p>
<p>This may not be the way I would design the validation system today, but it was an excellent choice at the time.</p>
]]></content:encoded>
			<wfw:commentRss>http://couchware.ca/www/kev/2009/02/02/using-custom-attributes-for-validation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
