Towards an AHAH framework / automatic javascript generation

Events happening in the community are now at Drupal community events on www.drupal.org.
starbow's picture

I have been having some really exciting ideas about getting the formAPI to generate the bindings to the javascript. I realized that all the javascript I wrote for the tutorial module could be generalized. Then with a bit of glue code, all of the parameters needed to do the event binding could be put in a properties set on the form element. This way Drupal coders could create modules with sophisticated AHAH effects without needing to write any Javascript at all. Here is an example of a button declaration with all of the info needed to do a incremental page update:

<?php
   $element
['new_subwidget']['add_subwidget_button'] = array (
       
'#type' => 'button',
       
'#value' => t( 'Add New Subwidget' ),
      
'#id' => 'widget_add_subwidget_button_' . $name,
      
'#ahah_bindings' => array (
                array(
                   
'wrapper' => 'widget_wrapper_' . $name,
                   
'event' => 'click',
                   
'path' => 'widget/widget_update_js',
                   
'params' => array( 'widget_name_js' => $name ),
                ),
       ),
);
?>

I have rewritten my widget.module to use this generalized framework and it all works and is pretty simple. All the magic happens in the ahah.module, which is still a little clunky, but it works. I think this approach could make creating dynamically updating forms for Drupal much easier and more approachable. You still need to set up the php correctly, but you don't have to wrap you brain around the js data model.

You can download the modified code and the ahah.module at: http://starbowconsulting.com/files/widget_0.zip

Comments

very interesting...

bcn's picture

I am going to d/l and test this soon, but I think you've hit onto a great idea. I was wondering about how to do this specifically with some of the more core parts of drupal (ie with the local tasks menu, as mentioned here http://drupal.org/node/90875#comment-187230), and I think what you're showing here is just the beginning of some really nice potential features.

thanks for sharing this!

Javascript

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week