<?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>Carbon Five Community &#187; JavaScript / AJAX</title>
	<atom:link href="http://blog.carbonfive.com/category/javascript-ajax/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.carbonfive.com</link>
	<description></description>
	<lastBuildDate>Fri, 27 Aug 2010 00:24:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A few experiments with HTML 5 applications</title>
		<link>http://blog.carbonfive.com/2010/07/software-design/a-few-experiments-with-html-5-applications</link>
		<comments>http://blog.carbonfive.com/2010/07/software-design/a-few-experiments-with-html-5-applications#comments</comments>
		<pubDate>Thu, 08 Jul 2010 21:30:03 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[spa]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=1052</guid>
		<description><![CDATA[I&#8217;ve had a long-standing interest in taking client-side programming beyond display logic and input validation. The new HTML 5 technologies are making full-scale application development in the browser increasingly practical. Unfortunately, the needs of Carbon Five&#8217;s clients generally exceed the capabilities of purely client-side solutions, so I don&#8217;t get to spend as much time developing [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had a long-standing interest in taking client-side programming beyond display logic and input validation.  The new <a href="http://en.wikipedia.org/wiki/HTML5">HTML 5</a> technologies are making full-scale application development in the browser increasingly practical. Unfortunately, the needs of Carbon Five&#8217;s clients generally exceed the capabilities of purely client-side solutions, so I don&#8217;t get to spend as much time developing JavaScript as I&#8217;d like. Consequently, I challenged myself to solve as many of my day-to-day issues as possible by writing applications that require only a single HTML page and a modern browser to run. So far, this has resulted in five applications; they&#8217;re available &#8212; with some additional thoughts on this style of development &#8212; below the fold.</p>
<p><span id="more-1052"></span></p>
<h2>The Applications</h2>
<ul>
<li>
  <b><label><a href="http://carbonfive.github.com/html5-playground/snowballs.html">Snowballs</a></label></b></p>
<p>Snowballs is a video game that was mostly designed by my 7 year old daughter, Ada. She started with a drawing that contained a maze filled with dots, a player and some monsters, and she explained how you throw snowballs at the monsters and build walls to block them. I convinced her we could get roughly the same effect by freezing the monsters instead of blocking them, and then I began developing. We started iterating, and she continued to contribute ideas which, somewhat surprisingly, always made the game more fun. The arrow keys move the player, and the spacebar launches a snowball. It&#8217;s implemented as a single Canvas tag using a JavaScript interval as a run loop. The maze, players, monsters, and snowballs are all drawn with JS functions using the Canvas drawing API. Since this application uses the Canvas tag, it won&#8217;t run in Internet Explorer or older versions of other browsers.
  </p>
</li>
<li>
  <b><label><a href="http://carbonfive.github.com/html5-playground/flashcards.html">Flashcards</a></label></b></p>
<p>I wrote Flashcards one evening when Ada&#8217;s teacher suggested using flash cards to help her memorize the words she shouldn&#8217;t need to sound out. The idea is that the parent clicks the spacebar when the child reads the word and they work together to reduce the time needed to read all the words. The application doesn&#8217;t need any graphics (other than some external images at the end), so it doesn&#8217;t use any HTML 5 features (browser compatibility is unknown).
  </p>
</li>
<li>
  <b><label><a href="http://carbonfive.github.com/html5-playground/timetest.html">Timetest</a></label></b></p>
<p>
    Like Flashcards, I wrote Timetest to help Ada with her schoolwork. In this case, she was getting frustrated with a lesson on analog clocks, so I wrote this application to make it more of a game. The goal is to answer as many questions as possible in the given time. The test turned out to be more difficult than it really needed to be, so I created an easier version (accessed by adding <i>#easy</i> to the URL). Since this application uses the Canvas tag, it won&#8217;t run in Internet Explorer or older versions of other browsers.
  </p>
</li>
<li>
  <b><label><a href="http://carbonfive.github.com/html5-playground/arithmetic.html">Arithmetic</a></label></b></p>
<p>
    Arithmetic is another educational application designed to drill addition that uses carrying. It provides spaces at the top, which you can click to mark a carry from the previous column. It doesn&#8217;t have any fancy HTML 5 features, but it does have a lot of stats.
  </p>
</li>
<li>
  <b><label><a href="http://carbonfive.github.com/html5-playground/surround.html">Surround</a></label></b></p>
<p>
    My wife and I had designed a fireplace surround and mantel for our living room,  but when it came to actually buying the materials and building the thing, we realized we didn&#8217;t know exactly how to scale it.  Since the design was entirely composed of rectangles, I realized it would be possible to build a simple one-off CAD program to help us agree on some dimensions.  The application makes use of the Canvas tag and the new HTML 5 slider inputs (which aren&#8217;t currently supported in Firefox). It translates between pixels and feet/inches constrained to an 8th of an inch, in order to make the measurements easier to use in the shop.
  </p>
</li>
</ul>
<p>  The goal of these exercises was to produce something simple and quick, but not entirely without polish. All the applications except Snowballs took only an evening or two. Each application (also excluding Snowballs) ended up under 300 lines of HTML, CSS, and JS combined.</p>
<h2>Developing without a JavaScript framework</h2>
<p>Limiting the applications to a single page means no importing <a href="http://jquery.com">jQuery</a> or <a href="http://www.prototypejs.org/">Prototype</a>. This might seem like a big constraint in an application written entirely in JavaScript. But these frameworks really only help out with DOM traversal/manipulation, event handling, animation, Ajax, form validation. All the applications make do with a small amount of HTML, so it&#8217;s easy enough to grab elements by ID, and a few visits to <a href="http://www.quirksmode.org/">Quirksmode</a> gave me some reasonably cross-browser event-handling code. Animation isn&#8217;t really that hard in JS, and I needed it to be more custom than frameworks allow anyway. The rest I didn&#8217;t need.  The truth is, I never miss having a framework (though I&#8217;m not about to stop using jQuery when working on client&#8217;s sites).</p>
<h2>Advantages and disadvantages</h2>
<ul>
<li>
  <b>They&#8217;re quick to start.</b>  Since it&#8217;s a single page, there&#8217;s no setup and no integration.
  </li>
<li>
    <b>They&#8217;re easy to deploy.</b>  It&#8217;s only one relatively small file, so you can e-mail it to friends, and you can host it anywhere. If you want to spend a little time making it touch-screen compatible, you can get it on all the smarter smart phones without bothering with app stores. On the other hand, there is no way prevent people from stealing the application or the code if you were developing it commercially, since the source is always just a right-click away.
  </li>
<li>
    <b>HTML+CSS is a powerful display framework.</b>  Although it lacks built-in support for some of the fancier UI widgets, there is no GUI framework that I&#8217;m aware of (and I&#8217;ve developed with quite a few) that provides the same level of design control with the same ease of development as these technologies. Of course, I&#8217;ve been swimming in HTML/CSS for years, so this opinion is a bit biased.
  </li>
<li>
    <b>Canvas has a powerful vector graphics API.</b>  I had done a fair amount of <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/Introduction.html">Cocoa programming</a> prior to these projects, so this didn&#8217;t come as a surprise. The real power of <a href="https://developer.mozilla.org/en/drawing_graphics_with_canvas">Canvas</a> lies in its transformation functions. I&#8217;d like to find an application for some more advanced spline generation. The downside is that Canvas and HTML don&#8217;t really mix, so you can have great text layout in HTML and sophisticated text animations in Canvas, but not both at the same time.
  </li>
<li>
    <b>JavaScript is a capable if not optimal language.</b>  No one is going to <a href="http://www.crockford.com/javascript/javascript.html">make the case</a> that JavaScript is the greatest language ever designed, but what it lacks in performance and language features, it partially makes up in powerful fundamentals and simplicity.  JavaScript is neither a great functional language nor a great object-oriented one, but it is sufficiently powerful to develop in either of these paradigms. Snowballs, for example, acts a lot like a simulation and benefited from stateful objects. The other applications required less state and I found several uses for <a href="http://en.wikipedia.org/wiki/Higher-order_function">higher-order functions</a>, though I wouldn&#8217;t consider any of them properly functional.
  </li>
<li>
    <b>Single-page applications are fun.</b>  These exercises have reminded me that, once the responsibilities of software engineering are stripped away, there is a joy to be found in coding for coding&#8217;s sake.
  </li>
</ul>
<p>
All of these applications are <a href="http://github.com/carbonfive/html5-playground">available on GitHub</a>, but it&#8217;s probably easier to right-click on the page and choose <i>view source</i>. <img src='http://blog.carbonfive.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/07/software-design/a-few-experiments-with-html-5-applications/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A neat javascript hack</title>
		<link>http://blog.carbonfive.com/2010/05/javascript-ajax/a-neat-javascript-hack</link>
		<comments>http://blog.carbonfive.com/2010/05/javascript-ajax/a-neat-javascript-hack#comments</comments>
		<pubDate>Fri, 14 May 2010 18:31:04 +0000</pubDate>
		<dc:creator>lowell</dc:creator>
				<category><![CDATA[JavaScript / AJAX]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=1001</guid>
		<description><![CDATA[Javascript library publishers often go out of their way to prevent other sites from linking to their libraries directly, preferring anyone who wants to use their library to download and host the file(s) themselves. The most common way to do this is to check the referrer of any requests for javascript files and reject any [...]]]></description>
			<content:encoded><![CDATA[<p>Javascript library publishers often go out of their way to prevent other sites from linking to their libraries directly, preferring anyone who wants to use their library to download and host the file(s) themselves. The most common way to do this is to check the referrer of any requests for javascript files and reject any requests which come from other domains. However, this requires the (minor) extra complexity of requiring server side support.</p>
<p>I just discovered a neat hack in <a href="http://www.crockford.com/">Douglas Crockford</a>&#8216;s popular json2 parser/serializer which obviates the need for any server side support. So simple, yet so effective. Check out the first line of  <a href="http://www.json.org/json2.js">http://www.json.org/json2.js</a>:</p>
<pre>  alert('IMPORTANT: Remove this line from json2.js before deployment.');</pre>
<p>Neat, eh?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/05/javascript-ajax/a-neat-javascript-hack/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Controlling embedded Vimeo video using javascript</title>
		<link>http://blog.carbonfive.com/2010/04/javascript-ajax/controlling-embedded-vimeo-video-using-javascript-3</link>
		<comments>http://blog.carbonfive.com/2010/04/javascript-ajax/controlling-embedded-vimeo-video-using-javascript-3#comments</comments>
		<pubDate>Thu, 01 Apr 2010 18:06:21 +0000</pubDate>
		<dc:creator>lowell</dc:creator>
				<category><![CDATA[JavaScript / AJAX]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=960</guid>
		<description><![CDATA[We are embedding video from Vimeo on a site we are working on and wanted to change the behavior of playing a video so that it would go back to the first frame after the video was done playing, rather than staying on the last frame. Since it ended up being harder than expected and [...]]]></description>
			<content:encoded><![CDATA[<p>We are embedding video from Vimeo on a site we are working on and wanted to change the behavior of playing a video so that it would go back to the first frame after the video was done playing, rather than staying on the last frame. Since it ended up being harder than expected and finding good documentation online was difficult, I thought I&#8217;d share my solution here.<span id="more-960"></span></p>
<p>First, in order to do this, you need to embed your video using the <a href="http://code.google.com/p/swfobject/">swfobject</a> javascript library. Next, you have to create a div that has an id (swfobject requires you to pass an id string for its initialization) &#8211; this div will be replaced by swfobject with a video object. The convention we&#8217;re using is to use an id of video-XXX, where XXX is the id of the video in <a href="http://vimeo.com">vimeo</a>&#8216;s system. Finally, the div needs to be given a size (using CSS) so that the embedded video will end up being the correct size.</p>
<p>The code is structured as a <a href="http://jquery.com">jQuery</a> plugin, so of course you will need jQuery. However, there is not a strong dependency on jQuery so with a little restructuring, I&#8217;m sure the jQuery dependency could be dropped.</p>
<p>Here it is:</p>
<p><script src="http://gist.github.com/352152.js?file=gistfile1.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/04/javascript-ajax/controlling-embedded-vimeo-video-using-javascript-3/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ada Freezes Monsters with Snowballs (and Canvas)</title>
		<link>http://blog.carbonfive.com/2010/03/javascript-ajax/ada-freezes-monsters-with-snowballs-and-canvas</link>
		<comments>http://blog.carbonfive.com/2010/03/javascript-ajax/ada-freezes-monsters-with-snowballs-and-canvas#comments</comments>
		<pubDate>Mon, 15 Mar 2010 17:53:32 +0000</pubDate>
		<dc:creator>alon</dc:creator>
				<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[sxsw canvas flash]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=911</guid>
		<description><![CDATA[Alex sent me an email Saturday morning with this really cute Canvas game he wrote with his daughter Ada. His timing was really great since I am on a panel at SXSW Tuesday called Is Canvas the End of Flash? He explains it all below&#8230; I was telling Ada how video games are made one [...]]]></description>
			<content:encoded><![CDATA[<p>Alex sent me an email Saturday morning with this really cute Canvas game he wrote with his daughter Ada. His timing was really great since I am on a panel at SXSW Tuesday called <a href="http://blog.carbonfive.com/2010/03/speaking/alon-on-flash-and-canvas-at-sxsw">Is Canvas the End of Flash?</a> He explains it all below&#8230;</p>
<blockquote><p>
I was telling Ada how video games are made one night, and she ask me if I could make a game. I said yes, we could make one together, and she bugged me all night to get started.  I told her to start by designing a game on paper.  She drew something that looks surprisingly like pacman (given that she&#8217;s never played the game), but she told me the dots were snowballs and that you could throw them at the monsters and use them to build walls.  I started programming it as an SPA using canvas.  She came up with all the rules, with some suggestions from me.</p>
<p>It may be that I&#8217;ve just been doing a lot of JS lately, but building a game like this in JavaScript felt really comfortable and extremely rapid.  I had a basic version working with about 200 lines of code, but the details have tripled that.  I went through a couple of major refactors, neither of which were troublesome in textmate.</p>
<p>The game is completely un-optimized, but it runs fast enough on my macbook.  I know lots of ways to speed it up if I had to.  It&#8217;s a little rough in a lot of areas, but it&#8217;s getting close enough to present.
</p></blockquote>
<p><a href="http://asalant.github.com/snowballs/" target="_new"><img src="http://blog.carbonfive.com/wp-content/uploads/2010/03/screenshot.png" alt="Play Snowballs" title="Play Snowballs" width="600" height="360" class="aligncenter size-full wp-image-916" /></a></p>
<p style="text-align: center">
<a href="http://asalant.github.com/snowballs/" target="_new">Play Snowballs</a>
</p>
<h3>Instructions</h3>
<p>Load in Firefox or Safari.  The arrow keys move your character, the spacebar launches snowballs (which you have to collect).  Hit a monster 5 times to freeze it, then touch it to collect it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/03/javascript-ajax/ada-freezes-monsters-with-snowballs-and-canvas/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Testing Talk in Oakland</title>
		<link>http://blog.carbonfive.com/2010/03/software-design/javascript-testing-talk-in-oakland</link>
		<comments>http://blog.carbonfive.com/2010/03/software-design/javascript-testing-talk-in-oakland#comments</comments>
		<pubDate>Mon, 08 Mar 2010 17:40:13 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[Speaking]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=903</guid>
		<description><![CDATA[Next week at EBig Jonah and I are wrapping up our world tour of talking about Javascript testing. March 17th in Oakland: &#8221;Recent evolutions in Javascript testing frameworks now allow creating test suites, test-driving development, and running tests on a continuous integration server. This allows us to support more complex Javascript, have confidence in the implementation, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ebig.org/index.cfm?fuseaction=Calendar.eventDetail&amp;eventID=158" target="_blank">Next week at EBig</a> Jonah and I are wrapping up our world tour of talking about Javascript testing. March 17th in Oakland: &#8221;Recent evolutions in Javascript testing frameworks now allow creating test suites, test-driving development, and running tests on a continuous integration server. This allows us to support more complex Javascript, have confidence in the implementation, and push more of the logic from the server into the browser, reducing the load on the server.&#8221; The focus of the talk is walking through a suite of tests we build for a real-world example.</p>
<p>For those of you who caught it last week at the SDForum, here are the links people requested:</p>
<ul>
<li><a href="http://code.google.com/p/javascript-test-maven-plugin/">Maven plugin for Javascript Unit Testing with Rhino/ScrewUnit</a> (from C5)</li>
<li><a href="http://github.com/ndp/wizardize">The current code for the &#8220;wizardize&#8221; example we walked through</a>.</li>
<li><a href="http://github.com/relevance/blue-ridge">Blue Ridge (Rails integration tool)</a></li>
<li><a href="http://pivotallabs.com/users/nick/blog/articles/455-better-javascript-testing-through-screwunit">ScrewUnit</a></li>
<li><a href="http://www.slideshare.net/jeresig/understanding-javascript-testing">JResig&#8217;s Presentation on JS Testing</a></li>
<li><a href="http://dl.dropbox.com/u/1399422/Javascript%20Unit%20Testing%2C%20Yes%20We%20Can%20Slides.pdf">The slides we used</a></li>
</ul>
<p>To sign up for next Wednesday, go to the <a href="http://www.ebig.org/index.cfm?fuseaction=Calendar.eventDetail&amp;eventID=158">EBig site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/03/software-design/javascript-testing-talk-in-oakland/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test-Driven JavaScript with ScrewUnit and BlueRidge</title>
		<link>http://blog.carbonfive.com/2010/02/software-design/test-driven-javascript-with-screwunit-and-blueridge</link>
		<comments>http://blog.carbonfive.com/2010/02/software-design/test-driven-javascript-with-screwunit-and-blueridge#comments</comments>
		<pubDate>Fri, 26 Feb 2010 01:33:04 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Ruby (on Rails)]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=893</guid>
		<description><![CDATA[Jonah and I are taking our presentation about Javascript Testing on the road next Tuesday at 6:30 in Palo Alto, at the SDForum The teaser for it&#8230; Recent evolutions in JavaScript testing frameworks now allow creating test suites, test-driving development, and running tests on a continuous integration server. This allows us to support more complex JavaScript, [...]]]></description>
			<content:encoded><![CDATA[<p>Jonah and I are taking our presentation about Javascript Testing on the road <a href="http://www.sdforum.org/index.cfm?fuseaction=Calendar.eventDetail&amp;eventID=13629&amp;pageId=471">next Tuesday at 6:30 in Palo Alto, at the SDForum</a></p>
<p>The teaser for it&#8230; Recent evolutions in JavaScript testing frameworks now allow creating test suites, test-driving development, and running tests on a continuous integration server. This allows us to support more complex JavaScript, have confidence in the implementation, and push more of the logic from the server into the browser, reducing the load on the server&#8230;</p>
<p>Hope to see you there.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/02/software-design/test-driven-javascript-with-screwunit-and-blueridge/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Screw.Unit JS Testing in Maven: javascript-test-maven-plugin</title>
		<link>http://blog.carbonfive.com/2010/02/testing/screw-unit-js-testing-in-maven-javascript-test-maven-plugin</link>
		<comments>http://blog.carbonfive.com/2010/02/testing/screw-unit-js-testing-in-maven-javascript-test-maven-plugin#comments</comments>
		<pubDate>Thu, 25 Feb 2010 03:29:31 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=878</guid>
		<description><![CDATA[I&#8217;ve written a maven plugin to integrate Screw.Unit javascript tests into a maven build. The project is inspired by the Blue Ridge testing framework for Rails, but it&#8217;s a bit more light-weight by design. To use the plugin you should start by downloading Screw.Unit and implement your tests according to their instructions. Once you have [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written a <a href="http://code.google.com/p/javascript-test-maven-plugin/">maven plugin</a> to integrate <a href="http://github.com/nkallen/screw-unit">Screw.Unit</a> javascript tests into a maven build.  The project is inspired by the <a href="http://blog.thinkrelevance.com/2009/5/12/blue-ridge-1-0-javascript-unit-testing-for-rails-scandalous">Blue Ridge</a> testing framework for Rails, but it&#8217;s a bit more light-weight by design.<br />
<span id="more-878"></span><br />
To use the plugin you should start by downloading Screw.Unit and implement your tests according to their instructions.  Once you have some tests implemented in html files, add the plugin to your maven pom file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javascript-test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.carbonfive.javascript-test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javascript-test-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-beta1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>src/test/javascript/suite*.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You will probably also need to add Carbon Five&#8217;s public repository so that maven will know where to grab the plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>c5-public-repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Carbon Five Public Repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://mvn.carbonfive.com/public<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The &lt;configuration&gt; section contains <a href="http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html">standard</a> maven &lt;include&gt; and &lt;exclude&gt; tags to allow you to specify any Screw.Unit test suite html files you would like to include in your build.</p>
<p>Now when maven executes its test phase, it will include your javascript tests as well. Any test that fails will halt the build. When the test phase is complete the target directory will contain a &#8216;screw-unit&#8217; directory that contains the test reports. There will be two files for every file included in the test execution The first is an XML file named TEST-dir1.dir2.dir3.filename.html.xml that contains the test results for that file in a reasonable approximation of JUnit&#8217;s xml format. The other file will be named dir1.dir2.dir3.filename.html and will contain a sort of snapshot of the test suite&#8217;s html at the time the tests completed (the javascript will be removed and the css will be baked in).</p>
<p>Like Blue Ridge, the javascript-test-maven-plugin relies on the excellent <a href="http://ejohn.org/blog/bringing-the-browser-to-the-server/">env.js</a> project and the less-excellent <a href="http://www.mozilla.org/rhino/">Rhino</a> project to emulate a browser in Java.   Unfortunately this combination creates the possibility of discrepancies between the browser and build results, and the probability of relatively slow test execution.  To keep the simulation as realistic as possible, javascript-test-maven-plugin recreates the test environment for each Screw.Unit html file, so the more tests you can cram into a single file the faster your builds will run.</p>
<p>There are a few directions for future work.  I&#8217;d like for the plugin to handle other browser-based javascript testing frameworks.  Screw.Unit is popular, but seems suboptimal in many respects.  The only limiting factor here is the plugin&#8217;s ability to interpret the results.  If env.js could run in javascript interpreters other than Rhino, I&#8217;d like to give the the plugin the capability to fork out to <a href="http://www.mozilla.org/js/spidermonkey/">spidermonkey</a> for better compatibility and performance.  Modifying the plugin would be relatively easy, but env.js appears to have some significant dependencies on Java.</p>
<p>Check it out if you have a need for this kind of thing and let us know if you have any problems or suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/02/testing/screw-unit-js-testing-in-maven-javascript-test-maven-plugin/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Unit testing JavaScript with Blue Ridge</title>
		<link>http://blog.carbonfive.com/2009/11/testing/unit-testing-javascript-with-blue-ridge</link>
		<comments>http://blog.carbonfive.com/2009/11/testing/unit-testing-javascript-with-blue-ridge#comments</comments>
		<pubDate>Sat, 14 Nov 2009 04:19:19 +0000</pubDate>
		<dc:creator>jonah</dc:creator>
				<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=696</guid>
		<description><![CDATA[My current rails project is using Blue Ridge and the tool set it bundles together (Rhino, env.js, Screw.Unit, and Smoke) to write test driven JavaScript. The ability to write tests of my JavaScript, in JavaScript, as efficiently as I can test other languages has forced me to rethink how I structure my work and I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>My current rails project is using <a href="http://github.com/relevance/blue-ridge">Blue Ridge</a> and the tool set it bundles together (<a href="http://www.mozilla.org/rhino/">Rhino</a>, <a href="http://github.com/thatcher/env-js">env.js</a>, <a href="http://github.com/nkallen/screw-unit">Screw.Unit</a>, and <a href="http://github.com/andykent/smoke">Smoke</a>) to write test driven JavaScript. The ability to write tests of my JavaScript, in JavaScript, as efficiently as I can test other languages has forced me to rethink how I structure my work and I&#8217;m writing better code because of it.</p>
<p>There are a couple of different options for JS testing available at this point but Blue Ridge&#8217;s plugin is the first I have seen which made writing test driven code as easy as I have come to expect while still providing all the functionality I need. In particular;</p>
<ul>
<li>Running my tests in a browser is fast. Refreshing fixture pages to rerun tests is dramatically more efficient than waiting for selenium tests to cycle.</li>
<li>I can debug my tests as they run against their fixtures pages and inspect or manipulate the DOM at any point.</li>
<li>I have mock and fake objects available to test behavior and stub out dependencies.</li>
<li>My JS tests run in continuous integration, just like all the other tests.</li>
</ul>
<p>By writing fixtures pages and tests first I am immediately using all of my code in two locations and running it against slightly different DOMs. That has pushed me to encapsulate most of my code as jQuery plugins and think carefully about how much I depend on the page&#8217;s structure. I end up with terse selectors instead of using whatever attributes are readily available and a clear configuration point for my page&#8217;s behavior as I chain together plugin calls in $(document).ready().</p>
<p>We&#8217;d like to see what other developers have done and share what we have learned so far so Carbon Five is hosting an open discussion of JavaScript testing in our office this week.</p>
<p>Topic: Javascript Testing<br />
When: Tuesday Nov 17<br />
6pm: arrive/network<br />
6:30 &#8211; 8 presentation and discussion<br />
Where: Carbon Five office: <a href="http://maps.google.com/maps?f=q&#038;hl=en&#038;geocode=&#038;q=171+2nd+St+%23+4,+San+Francisco,+CA%E2%80%8E&#038;sll=36.738884,-118.432617&#038;sspn=13.579326,18.632813&#038;ie=UTF8&#038;hq=&#038;hnear=171+2nd+St,+San+Francisco,+California+94105&#038;z=14&#038;iwloc=r0">171 2nd Street, 4th Floor</a></p>
<p>Please <a href="https://spreadsheets.google.com/viewform?formkey=dDg5dl9zbHlhZXNpdzBxdVIzblhjN3c6MA">RSVP</a> if you would like to attend.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/11/testing/unit-testing-javascript-with-blue-ridge/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Agile Practices&#8230; visualized?</title>
		<link>http://blog.carbonfive.com/2009/10/javascript-ajax/agile-practices-visualized</link>
		<comments>http://blog.carbonfive.com/2009/10/javascript-ajax/agile-practices-visualized#comments</comments>
		<pubDate>Wed, 07 Oct 2009 16:19:35 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=684</guid>
		<description><![CDATA[Only pure agile devotees will find it interesting&#8230; Revisiting agile methodologies, I wanted to solidify my understanding of the differences between agile, scrum, XP, etc. I went through a mini-research project of reviewing the &#8220;canonical&#8221; sources of these practices, and then built a quick visualization to clarify my understanding: http://ndpsoftware.com/agile_methods/agile_methods.html Hint: Try dragging around the [...]]]></description>
			<content:encoded><![CDATA[<p>Only pure agile devotees will find it interesting&#8230; Revisiting agile methodologies, I wanted to solidify my understanding of the differences between agile, scrum, XP, etc. I went through a mini-research project of reviewing the &#8220;canonical&#8221; sources of these practices, and then built a quick visualization to clarify my understanding:</p>
<p><a title="Agile Methods Visualized" href="http://ndpsoftware.com/agile_methods/agile_methods.html">http://ndpsoftware.com/agile_methods/agile_methods.html</a></p>
<p><em>Hint: Try dragging around the boxes</em> to see how practices are related to each other.</p>
<p><em>Warning: If it doesn&#8217;t draw anything interesting for you, refresh your browser&#8230;</em> there&#8217;s a large component of &#8220;randomness&#8221; to the algorithm and it can get stuck easily.</p>
<p><em>Warning II: Don&#8217;t leave it running in your browser</em>, as it&#8217;s somewhat sluggish Javascript&#8230; this was a demo thrown together in a couple hours.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/10/javascript-ajax/agile-practices-visualized/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Evernote for the Palm Pre</title>
		<link>http://blog.carbonfive.com/2009/06/javascript-ajax/evernote-for-the-palm-pre</link>
		<comments>http://blog.carbonfive.com/2009/06/javascript-ajax/evernote-for-the-palm-pre#comments</comments>
		<pubDate>Wed, 24 Jun 2009 16:31:07 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Palm Pre]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=615</guid>
		<description><![CDATA[The Pre, an innovative next generation smartphone from Palm Inc., recently launched with much fanfare. Access to the WebOS SDK was limited to a few select partners including Evernote, maker of a web, mobile and desktop applications for capturing and finding our memories, notes and inspirations. The Pre and the app are getting a good amount press from tech heavyweights like lifehacker and ZDnet. We [...]]]></description>
			<content:encoded><![CDATA[<div>The Pre, an innovative next generation smartphone from Palm Inc., <a href="http://blog.evernote.com/2009/06/12/evernote-palm-pre/" target="_blank">recently launched</a> with much fanfare. Access to the WebOS SDK was limited to a few select partners including Evernote, maker of a web, mobile and desktop applications for capturing and finding our memories, notes and inspirations.</div>
<div>The Pre and the app are getting a good amount press from tech heavyweights like <a href="http://lifehacker.com/5288075/evernote-available-in-palm-pre-app-catalog" target="_blank">lifehacker </a>and <a href="http://blogs.zdnet.com/cell-phones/?p=1387" target="_blank">ZDnet</a>.</div>
<div>We worked with Evernote to develop the first release of <a href="http://www.evernote.com/about/download/palm/pre/" target="_blank">Evernote for the Palm Pre</a> using this new platform.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/06/javascript-ajax/evernote-for-the-palm-pre/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convenient CSS and Javascript in Ruby on Rails</title>
		<link>http://blog.carbonfive.com/2009/03/javascript-ajax/convenient-css-and-javascript-in-ruby-on-rails</link>
		<comments>http://blog.carbonfive.com/2009/03/javascript-ajax/convenient-css-and-javascript-in-ruby-on-rails#comments</comments>
		<pubDate>Mon, 09 Mar 2009 00:41:11 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Ruby (on Rails)]]></category>
		<category><![CDATA[prototyping]]></category>
		<category><![CDATA[rapid development]]></category>
		<category><![CDATA[Software Design]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=480</guid>
		<description><![CDATA[I get tired of hunting through a hierarchy of folders and files, from the views to the public folder, to locate a certain CSS or Javascript file. It&#8217;d be convenient to have them right with the markup, but embedding these definitions within your HTML markup is a bad idea for several reasons. For our current [...]]]></description>
			<content:encoded><![CDATA[<p>I get tired of hunting through a hierarchy of folders and files, from the views to the public folder, to locate a certain CSS or Javascript file. It&#8217;d be convenient to have them right with the markup, but embedding these definitions within your HTML markup is a bad idea for several reasons. For our current project, I proposed we put everything in the view directory so they are easy to find:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">app<span style="color: #339933;">/</span>
  views<span style="color: #339933;">/</span>
    home<span style="color: #339933;">/</span>
      index.<span style="color: #006633;">html</span>.<span style="color: #006633;">rb</span>
      index.<span style="color: #006633;">css</span>
      index.<span style="color: #006633;">js</span></pre></div></div>

<p>The convention is clear: to add page-specific CSS code, just create a new file with the same name as the view, in the same folder. Easy to add, edit and remove. The alternative (using the public folder), usually leads to a parallel hierarchy and the inconvenience of that.</p>
<p>I also thought, that this being Rails and all, the files should be included automatically. It turned out to be pretty easy.<span id="more-480"></span></p>
<h2>The Implementation</h2>
<h3>Serving the Assets</h3>
<p>To serve this file, we need a route and a controller.</p>
<p>First the route:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">map.<span style="color:#9900CC;">connect</span> <span style="color:#996600;color: blue;">'/asset/:path'</span>,
    <span style="color:#ff3333; font-weight:bold;">:controller</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;color: blue;">'asset'</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;color: blue;">'serve_asset'</span>, <span style="color:#ff3333; font-weight:bold;">:path</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*</span>.<span style="color:#006600; font-weight:bold;">&#40;</span>js<span style="color:#006600; font-weight:bold;">|</span>css<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span></pre></div></div>

<p>We need to be careful here, since we&#8217;re going to be serving files directly out of our view hierarchy&#8211; make sure the route only picks up the javascript and CSS files.</p>
<p>The corresponding controller:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> AssetController <span style="color:#006600; font-weight:bold;">&amp;</span>lt; ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> serve_asset
    path = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:path</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#CC0066; font-weight:bold;">format</span> = path.<span style="color:#CC0066; font-weight:bold;">sub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*</span>.<span style="color:#006600; font-weight:bold;">&#40;</span>w<span style="color:#006600; font-weight:bold;">+</span>$<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;color: blue;">'1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">js</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:file</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#996600;color: blue;">&quot;#{RAILS_ROOT}/app/views/#{path}&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">css</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:file</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#996600;color: blue;">&quot;#{RAILS_ROOT}/app/views/#{path}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now the assets mentioned above are served as /assets/index.css and /assets/index.js.</p>
<h3>Including the Assets</h3>
<p>With this convention, we added code to our layout that includes these files automatically, but only if they exist. First, the additional controller code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> AssetController <span style="color:#006600; font-weight:bold;">&amp;</span>lt; ApplicationController
  ...
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">include_asset</span><span style="color:#006600; font-weight:bold;">&#40;</span>rel_path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#996600;color: blue;">''</span> <span style="color:#9966CC; font-weight:bold;">unless</span> AssetController.<span style="color:#9900CC;">asset_exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>rel_path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> rel_path
      <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">js</span>$<span style="color:#006600; font-weight:bold;">/</span>
        <span style="color:#996600;color: blue;">&quot;&lt;script src=&quot;</span><span style="color:#006600; font-weight:bold;">/</span>asset<span style="color:#006600; font-weight:bold;">/</span><span style="color:#008000; font-style:italic;">#{rel_path}&quot; type=&quot;text/javascript&quot;&gt;&lt;!--mce:0--&gt;&lt;/script&gt;&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">css</span>$<span style="color:#006600; font-weight:bold;">/</span>
        <span style="color:#996600;color: blue;">&quot;
&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">asset_exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>rel_path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC00FF; font-weight:bold;">FileTest</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;color: blue;">&quot;/app/views/#{rel_path}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And the code needed in the (Erector) layout:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">head <span style="color:#9966CC; font-weight:bold;">do</span>
  c = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>
  rawtext AssetController.<span style="color:#9900CC;">include_asset</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;color: blue;">&quot;#{c.name.underscore}.js&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  rawtext AssetController.<span style="color:#9900CC;">include_asset</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;color: blue;">&quot;#{c.name.underscore}.css&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Actually, since it&#8217;s Erector, the views are Ruby classes, so there is a hierarchy. These assets may be specific to any one of these:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">c = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>
<span style="color:#9966CC; font-weight:bold;">while</span> c != <span style="color:#CC00FF; font-weight:bold;">Object</span>
  rawtext AssetController.<span style="color:#9900CC;">include_asset</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;color: blue;">&quot;#{c.name.underscore}.js&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  rawtext AssetController.<span style="color:#9900CC;">include_asset</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;color: blue;">&quot;#{c.name.underscore}.css&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  c = c.<span style="color:#9900CC;">superclass</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h2>Discussion</h2>
<p>Although some people will bristle at the proliferation of files, I have used this technique repeatedly over the last five years (in Java and PHP projects). I find it leads to sound, modular development. With Ruby, it&#8217;ll be pretty straightforward to concat the assets together later on, to reduce requests, or even embed the code directly in the served HTML files, if it&#8217;s small enough. It&#8217;s also convenient to add page caching in the controller, when the time comes. Adding</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  caches_page<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:serve_asset</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>copies all these files into public/assets (as they are accessed), which conveniently takes your mongrel (or whatever) out of the picture.</p>
<p>You might imagine this keeps you from organizing and structuring your CSS and Javascript; it doesn&#8217;t do that. It lowers the barier to using a &#8220;local&#8221; scope for the CSS and Javascript. It allows you to write page-specific CSS and Javascript, which if you&#8217;re developing code quickly, is a lifesaver. We have repeatedly opened a view-specific Javascript file, seen that it no longer applies to the code, and removed it&#8211; safely. It&#8217;s easy to edit scripts when you know they only effect one page. Without this, I tend to lump things together that don&#8217;t really belong, and it becomes hard to tease them apart later.</p>
<p>Thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/03/javascript-ajax/convenient-css-and-javascript-in-ruby-on-rails/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Absolutize for jQuery</title>
		<link>http://blog.carbonfive.com/2008/09/javascript-ajax/absolutize-for-jquery</link>
		<comments>http://blog.carbonfive.com/2008/09/javascript-ajax/absolutize-for-jquery#comments</comments>
		<pubDate>Thu, 11 Sep 2008 22:34:15 +0000</pubDate>
		<dc:creator>james</dc:creator>
				<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=194</guid>
		<description><![CDATA[We&#8217;re using jQuery for one of our current projects. Today I found myself in an IE situation that could be solved by using the prototype librarie&#8217;s absolutize method. I couldn&#8217;t find any equivilent implementation that I liked in jQuery so I went ahead and ported absolutize from prototype to jquery. Download Here It can be [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re using jQuery for one of our current projects. Today I found myself in an IE situation that could be solved by using the prototype librarie&#8217;s <a href="http://www.prototypejs.org/api/element/absolutize">absolutize method</a>. I couldn&#8217;t find any equivilent implementation that I liked in jQuery so I went ahead and ported absolutize from prototype to jquery.</p>
<p><a href="http://plugins.jquery.com/project/absolutize_prototype_port">Download Here</a></p>
<p>It can be used in the standard jQuery way, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">'some-selector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">absolutize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">absolutize</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</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><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> element <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">'position'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;color: blue;">'absolute'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> element<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> offsets <span style="color: #339933;">=</span> element.<span style="color: #660066;">offset</span><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> top <span style="color: #339933;">=</span> offsets.<span style="color: #660066;">top</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> left <span style="color: #339933;">=</span> offsets.<span style="color: #660066;">left</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> width <span style="color: #339933;">=</span> element<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">clientWidth</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> height <span style="color: #339933;">=</span> element<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">clientHeight</span><span style="color: #339933;">;</span>
&nbsp;
    element._originalLeft <span style="color: #339933;">=</span> left <span style="color: #339933;">-</span> parseFloat<span style="color: #009900;">&#40;</span>element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">&quot;left&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    element._originalTop <span style="color: #339933;">=</span> top <span style="color: #339933;">-</span> parseFloat<span style="color: #009900;">&#40;</span>element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">&quot;top&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    element._originalWidth <span style="color: #339933;">=</span> element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">&quot;width&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    element._originalHeight <span style="color: #339933;">=</span> element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">&quot;height&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">&quot;position&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;color: blue;">&quot;absolute&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">&quot;top&quot;</span><span style="color: #339933;">,</span> top <span style="color: #339933;">+</span> <span style="color: #3366CC;color: blue;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">&quot;left&quot;</span><span style="color: #339933;">,</span> left <span style="color: #339933;">+</span> <span style="color: #3366CC;color: blue;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">&quot;width&quot;</span><span style="color: #339933;">,</span> width <span style="color: #339933;">+</span> <span style="color: #3366CC;color: blue;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;color: blue;">&quot;height&quot;</span><span style="color: #339933;">,</span> height <span style="color: #339933;">+</span> <span style="color: #3366CC;color: blue;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> element<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2008/09/javascript-ajax/absolutize-for-jquery/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Dojo drag and drop gotcha in IE</title>
		<link>http://blog.carbonfive.com/2007/06/javascript-ajax/dojo-drag-and-drop-gotcha-in-ie</link>
		<comments>http://blog.carbonfive.com/2007/06/javascript-ajax/dojo-drag-and-drop-gotcha-in-ie#comments</comments>
		<pubDate>Wed, 13 Jun 2007 17:21:37 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Dojo JavaScript AJAX drag-and-drop drag and drop bug issue IE Internet Explorer]]></category>

		<guid isPermaLink="false">http://blog/?p=16</guid>
		<description><![CDATA[I have been working on a web app for one of our clients that includes a pretty interactive AJAX / Web 2.0 component within it. This component is essentially a simplified drag-and-drop book publishing tool, which allows users to upload images and then to arrange them on predefined templates in order to create a book. [...]]]></description>
			<content:encoded><![CDATA[<p>
I have been working on a web app for one of our clients that includes a pretty interactive AJAX / Web 2.0 component within it.  This component is essentially a simplified drag-and-drop book publishing tool, which allows users to upload images and then to arrange them on predefined templates in order to create a book.
</p>
<p>
We chose to use the <a href="http://dojotoolkit.org/">Dojo Toolkit</a> (version 0.4.2) for both the AJAX (ie: remote calls to the server) and the Widegtry side of the Web 2.0 style.  It&#8217;s been working fairly well.  Dojo has a lot of functionality, but also has a few areas that still need some refinement.  The most interesting / infuriating to me so far has to do with Dojo drag-and-drop functionality within IE.
</p>
<p>
Dojo&#8217;s facility for drag-and-drop is essentially composed of three components:</p>
<ul>
<li>Drag-and-Drop Manager</li>
<li>Drag Sources</li>
<li>Drop Targets</li>
</ul>
<p>
It intuitively makes a lot of sense.  You create drag sources and drop targets and out of the box they actually work pretty well.  Dojo uses a form of JavaScript inheritance to allow you to create drag copy sources or drag move sources, each of which have different behaviors.  You can also reassign specific functions on your drag sources or drop targets in order to customize their behaviors.
</p>
<p>
I developed and debugged the app in Firefox and Firebug, as we all do.  But when it came time to check that everything works in IE, I ran in to one bug in particular that stumped me for two entire days, and frustrated me to no end.  At a certain point in using the app, my ability to drop objects on their drop targets was broken.  It only happened in IE (I was using IE 6 for Windows), and the error I got was the always helpful &#8220;Unexpected error&#8221;.
</p>
<p>
What I found was that the following code, which creates a drop target, also registers the drop target with the Drag and Drop Manager.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> imageTarget <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> dojo.<span style="color: #660066;">dnd</span>.<span style="color: #660066;">HtmlDropTarget</span><span style="color: #009900;">&#40;</span>imageDiv<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;color: blue;">'galleryImages'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
imageDiv.<span style="color: #660066;">imageTarget</span> <span style="color: #339933;">=</span> imageTarget<span style="color: #339933;">;</span></pre></div></div>

<p>
As a matter of fact, the Drag and Drop Manager, which is a global singleton, keeps a registry of all drag sources and drop targets created.  Later on in the flow of my application, some of these drop targets need to be re-rendered, and to do so I actually delete the DOM node and re-create it, using the following code:
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">dom</span>.<span style="color: #660066;">destroyNode</span><span style="color: #009900;">&#40;</span>pageContainerDiv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
The <b>destroyNode()</b> function is supposed to be memory-leak-friendly.  But after I have done that, and created a new drop target where that one once was, when I try to drop another drag source on to it, I get the &#8220;Unexpected error&#8221; exception in JavaScript.  And again, this only happens in IE.  Firefox works fine.  I finally realized that, even though the drop targets are automatically (and outside my knowledge for a long time) being registered with the Drag and Drop Manager, they must be explicitly unregistered before they are destroyed, otherwise this error occurs.  I added the following code to do so.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">dnd</span>.<span style="color: #660066;">dragManager</span>.<span style="color: #660066;">unregisterDropTarget</span><span style="color: #009900;">&#40;</span>imageDiv.<span style="color: #660066;">imageTarget</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
And now everything works perfectly!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2007/06/javascript-ajax/dojo-drag-and-drop-gotcha-in-ie/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
