When Ajax goes wrong

So I’ve been migrating the about-me widget over to the new widget API in WordPress 2.8.2+ for the past week and I’ve not been having fun.

Actually I’ve had the majority of the widget done, and simplified for more than half that time, but I’ve been struggling with one issue since; how to get the rich text from the tinyMCE instance into the textarea on form submit so it will be saved. And let me tell you… it’s been a tail of one annoying discovery after another.

First let me say that the new widget API is impressively simple and a breeze to work with, however in many ways it reminds me of the iPhone, fantastic so long as all you need to do is what the original engineers thought you would ever want to do. In other words it has some limitations, and they’re mainly around the wp-admin/widgets.php page itself instead of the api.

Where to start?

Each widget admin form is rendered in it’s own form, fantastic, but the form has no id or name, boo. It took me a ridiculous amount of time to figure out the correct jQuery foo to get the form encompassing the current widget so that I could try and add a submit hook. Which brings us to the next issue, try as I might I could not find a way to actually catch the form submit event and force the tinyMCE widget to copy the html to the textarea. I soon discovered that this was because the form is never action submited, in fact the widgets admin page wires the ajax request to the onclick event of the save button. That’s right, rather than adding an onsubmit event handler to catch the form and ajax-ify it, they use an onclick event. If they’d actually used the form submit event then the tinyMCE auto detection would have worked on it’s own.

So as it stands we have a whole bunch of html foms with no id’s or names, which are ajax-submitted by an onclick event of the button. Not the best way to do it, but workable. So the next step is to register an onclick event hook on the button using jQuery and just force the tinyMCE.triggerSave() call in there.

Nope. That won’t work either, since jQuery fires all the click event’s asynchronously and the generic form submission click event is bound before my custom event. There is no way in jQuery to reorder the event bindings, you can only unbind them and add your own. Dirty, dirty, dirty.

I’m going to digress now to talk about Anthem.Net, an ajax library that knew how to integrate on both the client and server. Anthem did ajax exactly as I’d expected it above, it caught the form submit event, built an asynch request, fired it off and then updated the page with the new content. This conversion of a .net form postback concept to an ajax mechanism was named a callback in Anthem terminology, and Anthem provided a way to catch the event on both the client and server. On the client side it provided another event called onprecallback (or similarly named), which would allow the developer to hook in any client side code that needed to be run before the form was submitted. I was sorely missing this feature while working on this widget.

So now to the widget, what I have to do is unbind the save button’s click event, bind my own event that does the tinyMCE.triggerSave() call and manually call whatever functions it was that the save button’s click event was meant to call.

Not a clean solution at all, but it works

Paul Graham does it again, on publishing

Fantastic read on publishing, and the concept of “paying for content” by Paul Graham.

Always an interesting take on things. I especially appreciated the distinction between “content” and software, both forms of information.

Cool windows utility to process log files

Over on the msmq blog, I found this awesome snippet about using the FIND command line utility to remove the noise from log files that your are looking in.

So for example, to ignore all lines with “0xC00E001B” in it you would use this command:

FIND  /V  "0xC00E001B"  MSMQ.LOG  >  NEWMSMQ.LOG

We’re hiring a front end dev at ninemsn

We’re currently looking for a front end developer at work at the moment.

To give a bit of context, we’re looking for a front end dev in the Web Campaigns dev team which forms part of the larger Commercial Development Team that I head up.

We’re looking for someone who is fresh, and into the latest and greatest of online, including but not limited to social media, rich user experiences (ajax, flash, silverlight) and absolutely knows their way around cross browser compatible site building. The prospective candidate also still has to have their chops as an actual developer, mainly c#, vb.net and SQL Server, but I want to be clear that the position is predominantly front end development not application development.

If you think that description above is you, then hit the jobvite link and send through your resume. We also just launched a careers page on our main portal, so if you’re in the market, but this role above isn’t quite you, check it out.

Cool text editor plugin

I love this zen coding plugin that expands css classifiers into html template text. Fantastic idea.

So you type this:

div#name.one.two

And the plugin expands to this

<div id="name" class="one two"></div>

Now I just need to find someone write the plugin for Notepad++, VS2008 or jEdit.

Where does the mobile internet live?

Interesting post on where the mobile internet lives over on the blog for the guys from 5th Finger International.

Funny point about the .mobi domain hardly being used at all. I personally disagree with segregating mobile content sites out. I hate to use the ‘C‘ word again, but so far as internet publishing is concerned, it’s all about Convergence.

And don’t get me started on all these iPhone specific sites. Wrong, wrong, wrong !!!

It’s like we didn’t learn from all the “Made for Internet Explorer” debacles over a decade ago. Sites targeted to a specific browser are wrong, I don’t care how sexy the concept seems now. We’ve already been down this road for the PC and it took us many, many years before we got to a point where the web was generally consumable by anyone on any PC browser.

I’m all for segmenting the mobile experience into down-level vs. up-level rendering, but this latest trend of going down-level vs. iPhone is the work of the devil I tell yah. Can’t we just skip all the teething problems that internet development went through over the last 10 years and jump straight into mobile web at the same maturity level as we are for PC?

Disclaimers:

  1. I own an iPhone and have hated it as a phone pretty much since I got it. (It’s a great convergence device though)
  2. In my role at ninemsn I head up the developer team for the Australian arm of 5th Finger.

Outlook add-in new explorer hook bug

Found this over on Noam Lampert’s blog, originally form monologue.

I remember running into this bug when writing a rather trivial outlook add-in that registered a toolbar on all new explorers. At the time I was so far under time pressure that I just caved and changed the toolbar installer to install it on all toolbars by default (worked around the bug). I didn’t think that it might be a garbage collection issue. Good spot by Noam and co.

Short version: Any new explorer callbacks will get garbage collected unless you actually create an explicit reference to the Application.Explorers that you manage in your add-in.

So you would replace this:

Application.Explorers.NewExplorer += new ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);

with this:

Explorers explorers = Application.Explorers;
explorers.NewExplorer += new ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);

Microsoft AJAX CDN

Over on ScottGu, this announcement about Microsoft setting up a CDN for ASP.Net Agax and jQuery.

Free for commercial and non-commercial use. A great idea for sites that heavily use jQuery or ASP.Net Ajax.

Monotouch for iPhone goes 1.0

According to miggie, Monotouch for iPhone goes 1.0.

This enables you to use a static version of .Net (actual mono) with native C# bindings of the iPhone API, so no Compact Framework concepts for iPhone just yet. I agree with this decision, since people developing for the iPhone want their apps to look like they were developed for the iPhone.

PHP Toolkit for ADO.Net Data Services

Found this interesting link on this post over on monologue.

Basically it’s a PHP toolkit to get access to the data exposed by ADO.Net Data Services, which exposes data in a RESTful manner.