$self had the clever idea that we could use the sort of new ‘Web 2.0′ buzzword paradigm of Ajax on the software $dayjob produces. All well and good, the boss approved, and the mock-up worked – a simple form, Prototype as the Ajax library and some PHP (5.2 no less) that returned a JSON string in the header. Adding records happened in the background, and on success, the page updated, a little status field updated, a reload of the entire data set from the database server wasn’t needed and life was groovy. All of the content validation occurred on the server side, and because it’s in a library, I only have to write one set of rules, regardless of whether it’s Ajax, standard forms, or even SOAP.
Part the next was to integrated the entire thing into the real code that drives our web interface. ‘No problem $self, you know it works…’ Well, who tell me think dat!? After some fiddling with the templating engine (which uses { and } heavily), and some re-writing and re-forming of the proof-of-concept code into a generic routine that could handle any of the configuration pages, I had a test page ready to rock and roll. The code set the Ajax URL target on the fly in the template, and Firebug reported that ‘Yes Bob, the data went out, and the response came back.’, but nary an update occurred on the page.
Over an hour later, after stripping the code down to barebones, making copious use of the debugger statement that Firebug can pick up, and at least 2 cups of tea, I realised two things.
- I had missed three semi-colons on code inside of the var myajax = new Ajax.Request( ); segment.
- I had used the wrong name for two functions, also called inside that block.
If you do that outside of the object creation, the JavaScript interpreter in the browser yells at you loudly. If you do it inside the object creation, it fails silently. Not an error anywhere. Not in Firebug. Not in Firefox’s error console. Nada. Not a sausage.
I now remember why I dislike JavaScript. Hopefully the Ajax book I ordered will give me some insight into how to deal with stupidity (on my part, and on the part of JavaScript) like that.