Signals and Slots is King
Wednesday, January 25, 2006Sigslots rocks my world. It saves me from having to mangle neat, abstracted code with convolluted call backs to get things to happen in the UI. I had a situation where I was creating an 'Ajax.AutoCompleter' for a field using the awsome script.aculo.us library and its Ajax.Autocompleter class. Problem was, the field was right above an iframe displaying a pdf, and the iframe obscured the auto completer's list display. I wanted a quick solution and I thought that if I could hide the iframe when the list was displayed and show it again when the list was hidden, that would be good enough.
In a traditional situation I'd have considered creating callback functions within the script driving the autocompleter, or somehow using event listeners on the keydown event for the field and the list element onlick event. Both would be messy. But with the awsome sigslots library I don't need to touch the autocompleter code at all. All I had to do was identify the functions that show and hide the list. In the Ajax.Autocompleter class they are render() and
hide() respectively.
I created 2 functions showIFrame() and
hideIFrame() that simply set the iframe's visibility style property to 'visible' or 'hidden'. Then I linked them like this:
var ajax = new Ajax.Autocompleter(...);
__sigslot__.connect(ajax, 'render', window, 'hideIFrame');
__sigslot__.connect(ajax, 'hide', window, 'showIFrame'); DONE!
The Ajax.Autocompleter code is untouched and the iframe hides and shows right on queue. No mess, no fuss.
While I love script.aculo.us, it really is missing a good event framework. Instead it relies on the Function.bind mechanism to create callbacks with closures, which is clunky in comparision. If such a competition existed, the Signals & Slots concept would win hands down!
I've written previously about the sigslot library, and was pleased to see Alex Russell, the man himself, posting a comment to say that his new project Dojo Toolkit has an event system that uses the sigslot concept. I think dojo deserves a closer look....
No comments yet