<?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>Dan's Drivelings &#187; Javascript</title>
	<atom:link href="http://dan.skaggsfamily.ws/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://dan.skaggsfamily.ws</link>
	<description>Random Thoughts of a Techno-Hermit</description>
	<lastBuildDate>Thu, 09 Sep 2010 02:37:19 +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>Be careful with the &#8216;local&#8217; scope when migrating from CF8 to CF9</title>
		<link>http://dan.skaggsfamily.ws/2010/02/08/be-careful-with-the-local-scope-when-migrating-from-cf8-to-cf9/</link>
		<comments>http://dan.skaggsfamily.ws/2010/02/08/be-careful-with-the-local-scope-when-migrating-from-cf8-to-cf9/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:24:15 +0000</pubDate>
		<dc:creator>Dan Skaggs</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://dan.skaggsfamily.ws/?p=355</guid>
		<description><![CDATA[One of the really nice &#8220;fixes&#8221; included in ColdFusion 9 from a developer&#8217;s perspective is the inclusion of an implicit &#8220;local&#8221; variable scope into which variables created within the body of a &#60;cffunction&#62; tag are placed by default. Previously, developers had to manually add a &#8220;var&#8221; keyword to variables that should only exist within the [...]]]></description>
			<content:encoded><![CDATA[<p>One of the really nice &#8220;fixes&#8221; included in ColdFusion 9 from a developer&#8217;s perspective is the inclusion of an implicit &#8220;local&#8221; variable scope into which variables created within the body of a &lt;cffunction&gt; tag are placed by default. Previously, developers had to manually add a &#8220;var&#8221; keyword to variables that should only exist within the confines of the function.</p>
<p>One of the ways of simplifying this that gained some traction among various developers prior to the release of ColdFusion 9 was to &#8220;var&#8221; a single variable at the top of the function as an empty structure then store any additional variables needed in the function inside it. Many folks, myself included, named this structure &#8220;local&#8221; so that it would be readily apparent that the values inside were local to that function. This approach worked fine and dandy on ColdFusion 8 and below.<br />
<span id="more-355"></span></p>
<p>I recently migrated one of my clients to ColdFusion 9 and not long after the client started getting isolated reports from his people having javascript errors in a data management application that uses AJAX-driven forms talking to CFCs.  At first these were very isolated and we weren&#8217;t able to reproduce the error, but, as time went on, the reports became more widespread. As I was troubleshooting this over the weekend, I discovered that we were getting javascript errors when trying to interact with this RemoteFacade CFC about 40% of the time.</p>
<p>Using <a href="http://www.getfirebug.com" target="_blank">Firebug</a>, I was able to watch the results come back from ColdFusion and noticed a very odd trend. Approximately 60% of the time, the JSON returned from the remote CFC call was as expected. In the other 40%, one of the main data structure names was an arbitrary, machine-generated name instead of the name we had specified in the code.</p>
<p>Here is an example of what we were expecting in the JSON returned from the RemoteFacade.cfc method:</p>

<div class="wp_syntax"><div class="code"><pre class="json" style="font-family:monospace;">{&quot;DATA&quot;:{&quot;CANEDIT&quot;:true,&quot;RECORDDATA&quot;:{&quot;Field1&quot;:&quot;value1&quot;} } }</pre></div></div>

<p>This is an example of what we would get back from the same request when we would see the javascript errors</p>

<div class="wp_syntax"><div class="code"><pre class="json" style="font-family:monospace;">{&quot;___IMPLICITARRYSTRUCTVAR5&quot;:{&quot;CANEDIT&quot;:true,&quot;RECORDDATA&quot;:{&quot;Field1&quot;:&quot;value1&quot;} } }</pre></div></div>

<p>See the difference?  Our &#8220;DATA&#8221; key was named completely differently which caused javascript to throw some error saying that variableName.DATA did not exist.</p>
<p>After looking over the ColdFusion code for quite a while and doing some step debugging with <a href="http://www.fusion-debug.com" target="_blank">FusionDebug</a> I had an idea.  I changed all the function-specific structures I had been using from:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #000000; font-weight: bold;">var</span> local <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">structNew</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
   ....
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfreturn</span> local <span style="color: #0000FF;">/&gt;</span></span></pre></div></div>

<p>to:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #000000; font-weight: bold;">var</span> ret <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">structNew</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
   ....
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfreturn</span> ret <span style="color: #0000FF;">/&gt;</span></span></pre></div></div>

<p>Once I changed those throughout the code base and reinitialized my application, all the javascript errors that we&#8217;d been experiencing across multiple request types went completely away.</p>
<p>I have an unconfirmed theory that in using the name &#8220;local&#8221; for my structure, ColdFusion was sometimes getting &#8220;confused&#8221; on what to return&#8211;ColdFusion&#8217;s built-in local scope or the method-specific variable I had named local. I don&#8217;t really have any way to prove that beyond a shadow of a doubt, but when I made the change, all my errors went away, so I decided chalk it up as a lesson learned for future development and move on to the next problem.  Needless to say the client was happy that the issue was fixed and I can say I learned something that day.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.skaggsfamily.ws/2010/02/08/be-careful-with-the-local-scope-when-migrating-from-cf8-to-cf9/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>jQuery head-scratcher and lesson learned</title>
		<link>http://dan.skaggsfamily.ws/2010/02/01/jquery-head-scratcher/</link>
		<comments>http://dan.skaggsfamily.ws/2010/02/01/jquery-head-scratcher/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 15:42:02 +0000</pubDate>
		<dc:creator>Dan Skaggs</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ModelGlue]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://dan.skaggsfamily.ws/?p=343</guid>
		<description><![CDATA[As I mentioned in my last post, I&#8217;m working on a pet project in my spare time. It uses jQuery in various places including in the site design template that I purchased to use with the site. The template uses jQuery to expand and collapse menu items in the left sidebar to show sub items [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in my <a href="http://dan.skaggsfamily.ws/2010/01/31/coldfusion-9-caching-settings-to-watch-out-for">last post</a>, I&#8217;m working on a pet project in my spare time. It uses <a href="http://www.jquery.com" target="_blank">jQuery</a> in various places including in the site design template that I purchased to use with the site. The template uses jQuery to expand and collapse menu items in the left sidebar to show sub items for that selection. Because of this, jQuery and a javascript file named custom.js was included in the template.  After breaking the template apart to work inside my <a href="http://www.model-glue.com" target="_blank">ModelGlue</a> application, I started implementing some other features that used jQuery with their own associated javascript files, one of which was <a href="http://cfuniform.riaforge.org" target="_blank">cfUniform</a>.</p>
<p>As soon as I put the cfUniform code into the page, I started getting javascript errors in the console pane of Firebug. The error would state something similar to &#8220;$(document) not a function&#8221; or &#8220;$ not a function&#8221;.  Now, I&#8217;ve not had a ton of experience with jQuery, but I have used several pre-built jQuery plugins in sites before and I had seen errors similar to this.  Normally this error is caused by one of two issues.  Either a) you&#8217;ve forgotten to include the script block to load the jQuery library or b) your code is loading the jQuery library twice.</p>
<p>I was able to use <a href="http://www.getfirebug.com" target="_blank">Firebug</a> to verify that I was indeed loading it and loading it only once but couldn&#8217;t for the life of me figure out why I was getting an error. Obviously cfUniform wasn&#8217;t really at fault (the error pointed to a line in one of the cfUniform javascript files) so I knew it had to be something on my side.  I did some searching on the phrase and found some discussions around jQuery&#8217;s noConflict() feature that allows you to reference jQuery with a notation other than using the familiar &#8220;$&#8221;.  </p>
<p>After reading for a while, I opened the custom.js file that came with the site template and found the code below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">noConflict</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</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: #006600; font-style: italic;">//contents snipped</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Since I&#8217;m not using any other javascript libraries in this application that might conflict with using the &#8220;$&#8221; to access jQuery, I removed the noConflict() line, but that didn&#8217;t fix my problem. On a hunch I did a search/replace through the custom.js file replacing &#8220;jQuery(&#8221; with &#8220;$(&#8221; so that references to the jQuery library in this file would be accessed with the same syntax as in all the other javascript files. Lo and behold, all my errors in Firebug&#8217;s console went away and CFUniform began behaving as expected.</p>
<p>While I don&#8217;t understand all the underpinnings of why this worked, I&#8217;ll take it as my &#8220;lesson for the day&#8221; that in the future I need to always make sure that all the various jQuery plugins and code that is used in my applications need to reference the jQuery library with the same syntax.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.skaggsfamily.ws/2010/02/01/jquery-head-scratcher/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Free and easy way to play MP3 files in your web pages</title>
		<link>http://dan.skaggsfamily.ws/2009/04/26/free-and-easy-way-to-play-mp3-files-in-your-web-pages/</link>
		<comments>http://dan.skaggsfamily.ws/2009/04/26/free-and-easy-way-to-play-mp3-files-in-your-web-pages/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 22:53:47 +0000</pubDate>
		<dc:creator>Dan Skaggs</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[mp3]]></category>

		<guid isPermaLink="false">http://dan.skaggsfamily.ws/?p=187</guid>
		<description><![CDATA[One of my current clients recently commissioned me to write a feature for their site that allows visitors to listen to snippets of MP3 tracks that they sell directly from within the product&#8217;s &#8220;detail&#8221; page. In my particular situation, one of the requirements was to use the design commissioned by a separate design firm. In [...]]]></description>
			<content:encoded><![CDATA[<p>One of my current clients recently commissioned me to write a feature for their site that allows visitors to listen to snippets of MP3 tracks that they sell directly from within the product&#8217;s &#8220;detail&#8221; page. In my particular situation, one of the requirements was to use the design commissioned by a separate design firm. In doing some research I found a few different open source methods of accomplishing this. The one that turned out to be the best fit for our situation was an Adobe Flash-based application called <a href="http://www.varal.org/media/niftyplayer/">NiftyPlayer</a>. </p>
<p>NiftyPlayer is a free, open source Flash file that you can embed into your page and play MP3 files without worrying about what audio player is installed on the visitor&#8217;s machine. The best part for my situation is that the player is completely controllable through Javascript. That means that I could completely &#8220;hide&#8221; the UI provided by the Flash file by setting the height and width of the Flash object to zero and use custom javascript methods to craft my page to do exactly what I wanted it to do.<br />
<span id="more-187"></span><br />
Having a Javascript API to the Flash player object means that you can simply added the following code toplay a track:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;#&quot;
  onClick=&quot;niftyPlayer('previewPlayer').loadAndPlay('fileName.mp3')&quot;&gt;
      Play
&lt;/a&gt;</pre></div></div>

<p>NiftyPlayer also provides a way to register different types of events and pass a reference to a custom block of Javascript code to be run with that event fires.  For example, you can register the &#8220;onPlay&#8221; event to change the source of an image on the page when a track starts playing and then register the &#8220;onStop&#8221; and/or &#8220;onSongOver&#8221; event to change it back when the track is either stopped manually or runs to completion.</p>
<p>Having this ability to completely hide the UI of the Flash object and control it with Javascript code was key for us as it allowed us to use NiftyPlayer in a design that had been created by an outside design firm without having to dive into the .fla file and update the colors, fonts etc originally used by the author. Using the Javascript API, I was able to control the source of an image (play and stop images) as well as update the CSS class of the div containing the currently playing track while it was playing and reset it back to its previous state when the user pressed the stop button or when the track finished playing.</p>
<p>Because of Flash&#8217;s near-ubiquitous install base, NiftyPlayer is a great option to easily add MP3 playback capability to your web application and know that it&#8217;ll &#8220;just work&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.skaggsfamily.ws/2009/04/26/free-and-easy-way-to-play-mp3-files-in-your-web-pages/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
