AHAH bindings do not work with 2+ forms on a page.
| Project: | AHAH Forms Framework |
| Version: | 5.x-1.5-5 |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | starbow |
| Status: | reviewed & tested by the community |
The ahah module adds some javascript settings via drupal_add_js() whenever 1+ more ahah form element bindings are made to any given form. If you have more than one form on a page, things break. All the ahah examples only show one form on a page, so this doesn't show up in the samples.
The issue is that if you have more than one form, the ahah_forms_bind_form() function adds drupal js settings for the "basePath" and "module" properties each time it is called. If it's called once, these properties have single values set (OK), but when called twice (more than one form on a page), these properties have an additional set of values set for them which turn the property values into arrays and not single values (multiple drupal_add_js for settings for the same property are cumulative...). This then screws up the ahah added javascript functions which expect a single value for the basePath to submit the ajax request for, but get's an array back instead.
This patch (sorry for the lack of a patch file), ensures that the "basePath" and "module" properties only get set once allowing for binding to occur in X number of forms on a page.
function ahah_forms_bind_form( $form ) {
$bindings = array();
static $added_base_module_settings;
ahah_forms_scan_form_children( $form, $form['#id'], $bindings );
// drupal_set_message( "After Scan: Wrapper Bindings = " . dprint_r( $bindings, TRUE ) );
if( count( $bindings ) > 0 ) {
//add in required javascript files
$module_path = drupal_get_path('module', 'ahah_forms');
drupal_add_js("$module_path/lib/form.js"); // jquery plugin for easy ahah form submition
drupal_add_js("$module_path/ahah_forms.js", 'module', 'footer'); // put in footer, so it can listen to other js files set bindings (kludgy)
// add basePaths and module only once
if (!$added_base_module_settings) {
drupal_add_js(
array(
'ahah' => array(
'basePaths' => array( 'base' => base_path(), 'module' => $module_path )
),
),
'setting'
);
$added_base_module_settings = TRUE;
}
// add specific binding for the given form
drupal_add_js(
array(
'ahah' => array('bindings' => array( $bindings )),
),
'setting'
);
}
return $form;
}
#1
Thanks a lot for the patch ! I have been dealing with such issues for quite some times now with some ugly solutions (=hacks) and yours is really great. I am using it on a page with a lot of dynamic forms (one for each comments on the pages).
It should really get in the distributed module.
Thanks again
#2
Thanks for figuring this out. If I roll another version, I will add this in.
#3
Note that the way ahah wroks can be very nasty with front-end performance if you have a lot of forms on your page.
I have pages with up to 50 forms (ahah administration of the comments is cool :) ). As the javascript properties are attached to the DOM elements dynamically once the page is loaded it can freeze your browser for some time (and even fire you a "a script on this page is taking too long"). I have no real solution on how to solve this problem. The elements should come with the javascript already attached to them, but I guess that it would require quite a lot of modification for the module.
#4
here's an actual patch