14 July 2006

CFC's - not all that object oriented

I had this cracking idea to store repopulate data in CF flash forms using a cfc and storing it in the session scope, then using a remoting function to broadcast this data so the form fields could data bind to remote service. Didn't quite work.

My cfc basically looked like this:

<cfcomponent>

<cfset this.formData = StructNew() />

<cffunction name="getValues" returntype="remote">
<cfreturn this.formData />
</cffunction>

</cfcomponent>

I instantiated the above cfc in the onSessionStart function:

Session.objFormData = CreateObject("component","formData")

I then used the onRequestStart function to populate the Session based object (as per the previous post):

<cfloop list="#form.fieldnames#" index="item">
<cfset Session.objFormData.formData[item] = Evaluate(item) />
</cfloop>

This works fine and removes the need for an "action" page - just simply copy the form into the session until you need it (a bit wasteful, yes, but really quick code if you're in a hurry!)

You'd think the value returned by "this.formData" would be the data stored in the session instance. Not so... all that was returned was an empty structure! This was essentially the uninstantiated cfc with no data. Doing a cfdump of the session showed that the object and data were still in the session scope, but "this" did not return the values of the object in the session.

Copying form scope into session scope CFMX 7

I've just noticed a weird behaviour with the form scope in CFMX 7. I was trying to copy all the data in the form to a session variable e.g.

Session.formData = StructNew()
Session.formData = form

Do a <cfdump var="#session#"> immediately after that line and you see the form structure inside the session structure. However, I noticed that when I redirect onto another page without form scope, the Session.formData structure was empty - it didn't persist, even though it was copied to the Session scope!?

What must be happening is that a reference to the form scope is copied into the Session NOT the form data. When you move to another page where the form scope does not exist, then the data is not in the Session.

You must loop though form.fieldnames and set the data explicitly, like so:
<cfloop list="#form.fieldnames#" index="item">
<cfset Session.formData[item] = Evaluate(item) />
</cfloop>


I should try and remember to start posting more!

05 April 2006

Should I buy a Mac or PC?

I thought long and hard about this and it really didn't come down to a hardware decision at all. I'd buy a computer that ran on hamsters in wheels if it was easy to use and had all the capabilities I need.

The specs for the latest Toshiba Qosmio dual core machines are very simliar to the MacBook Pro: same CPU, same graphics cards etc., yet they're a few hundred quid cheaper. But with Macs, you get what you pay for: design, simplictiy and well intergrated components. However, I personally think the MacBook Pro is over priced, I don't really like the look of it and the keyboard is too small. It could have been made much bigger if they'd repositioned the speakers and reduced the size of the touch pad.

I've upgraded from a 2.33GHz P4 Sony Vaio notebook (which I'll still use) to the 17" iMac 1.8GHz Core Duo. The iMac is truly great. I don't think any PC would have given me the same feeling of money well spent. I'm converted. Not specifically to Macs, but OS X.

I can get all the apps I need on OS X, plus loads of free downloadable stuff - just like you can on Windows XP (although sometimes the open source stuff is hard to install).

You never know Microsoft could surprise us all in January with Windows Vista - a stable well designed Operating System... but i doubt it. If Steve Jobs had any sense he'd make next version of OS X run on PC's and release it in October 2006.

However, the thing that makes Windows so unreliable is the vast amount of hardware it has to support. OS X is designed for Macs with specific components, but under the cover Macs are looking more and more like PC's.

Still, I reckon the iMac is probably the best computer for its price at the moment.

29 March 2006

Audio Cue Points in Flash MX 2004 Pro / Flash 8

Simple: turn the audio file into a video file (FLV).

There are probably much better ways of adding audio cue points that I haven't discovered yet, but Flash MX 2004 or Flash 8 don't seem to have any classes / components to help you do this quickly. Flash DOES have a selection of Media Components that allow you to add cue points to mpg or flv videos.

Simply convert the audio to FLV by exporting it with QuickTime Pro or another audio/video tool and the Flash Video Encoder (I use Sorenson Squeeze for this). The Flash 8 video encoder even allows you to add Event or Navigation cue points to the audio.

Import the FLV into your flash movie using the MediaDisplay component to add cue points as you would with video.

There's also a tutorial here that uses FlashAmp, the "Lite" version costs $45. This is probably better than my solution (but not as quick) as it can include the volume and spectral properties of the sound.

28 March 2006

Setting up Apache, PHP, MySql and Eclipse for Mac OS X

A mate of mine wanted to get into web development and needed to know the best way to go about getting the right stuff on his Mac. I've chosen PHP as the scripting language because it's one of the easiest to install on the Mac. Follow the links below in order, and you should have it all up and running within an hour.

Apache Server
http://www.onlamp.com/pub/a/mac/2001/12/07/apache.html

Apache PHP plugin
http://www.entropy.ch/software/macosx/php/

Eclipse (code editor)
http://www.eclipse.org/downloads/

PHP Plugin for Eclipse
http://sourceforge.net/projects/phpeclipse/

Database
http://dev.mysql.com/doc/refman/5.0/en/mac-os-x-installation.html
http://developer.apple.com/internet/opensource/osdb.html

PHP reference & tutorials
http://www.w3schools.com/php/

24 March 2006

Getting responseXML to work in IE

Nightmare! This took me ages to solve, but was actually quite easy in the end.

I thought it'd be a great idea to have "live" validation on a registration page, that'd check the database to see if a username had already been chosen.

First I created a ColdFusion Component (CFC) as a web service which checks the user input against usernames in the database and returns a boolean true/false. The CFC returns a WDDX formatted XML packet containing the boolean value.

Assuming you have a response handler like the AJAX tutorial here: http://developer.mozilla.org/en/docs/AJAX, all you need to do is use http_request.responseXML to receive the XML content and then use the XML DOM properties and methods to extract the data.

This works fine in Mozilla browsers like FireFox / Safari, but not in IE, which produces an 'Object Expected' error.

This is because IE expects all XML responses to have: Content-Type: 'text/xml' in the response header. However, ColdFusion broadcasts the WDDX packet as 'text/html', and therefore using responseXML in the javascript handler will not work, as IE doesn't acknowledge this as XML data.

The simple way to fix this is to create an XML DOM object in the Javascript response handler and use the responseText property, like so:

if(window.ActiveXObject){ // If IE Windows
var XMLdoc = new ActiveXObject("Microsoft.XMLDOM");
XMLdoc.loadXML(http_request.responseText);
} else {
var XMLdoc = http_request.responseXML;
}

AJAX and other animals

I've recently jumped on the AJAX band wagon and had a stab at using the XMLHttpRequest() object in my applications. I followed a number of tutorials including:

http://developer.mozilla.org/en/docs/AJAX
http://dhtmlnirvana.com/ajax/ajax_tutorial/

I'm still to be wholly convinced about using AJAX to deliver pages, due to accessibility issues, but I do think that using the XMLHttpRequest() to get live data as a user interacts with the page is well worth getting stuck into. It's the way ahead.

There are alternatives to AJAX: Flash Remoting, the art of using Flash for the UI and web services (mainly written in ColdFusion) and Macromedia - sorry - Adobe Flex 2 offer a much more dynamic UI, but at a price! Flex 2 will set you back the best part of a grand when it's released sometime this summer.

Hi there!

Hi,

This is the first entry on this site... not terrible exciting for anyone reading it, but I've been meaning to build a website for 5 years and never got around to it! What the hell was I going to use as a domain name for starters? Anyway... it excites me in ways I'd rather not share on here.

So, let me explain what this site is about. Lapsus Mentis pretty much translates as "absent minded" and is what an Italian collegue of mine called me as she didn't know how to describe me in English. This blog, indeed this whole site, is my own personal web development reference, so I don't forget the stuff I've done, problems I've solved or all the other websites that have create tutorials and references.

I mostly develop sites in ColdFusion and use flash / flex fairly frequently, so I'm fairly savvy with Actionscript. I also no a fair bit of ASP, a dash of .NET and have dabbled with Java (mainly J2ME). I'm always learning new stuff - there's a lot to learn! In the future I'll be posting stuff about my trials with AJAX, Flex 2, and MVC's (Fusebox / model-glue).

Hopefully, my reference can become your reference. After most of what I've learnt over the past 5 years has been from forums and blogs like this, so it's high time I gave something back.