This patch makes it possible to use a custom success callbacks for AHAH request performed via ahah.js. This is completely optional and current default behaviour is not changed at all, but adds much more flexibilty to ahah.js and makes it usable in many more situations.

This patch adds a new, optional key to the #ahah element property: success_callback. If set, it has to be the name of a function in the Drupal.ahah.prototype.successCallbacks namespace. Then, the specified function is called instead of the default success callback (which remains exactly as it used to be: it inserts the new content into the page based on the 'wrapper' and 'method' settings).

I needed this for the searchable-modules-page-with-vertical-tabs-patch, and it will be useful in many other situations. Also, this moves ahah.js a bit more in the direction of ctools/views ajax stuff, which is a good thing (tm) IMO.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Frando’s picture

See http://drupal.org/node/396478 for an example on how this can be used.

quicksketch’s picture

FileSize
755 bytes

I understand where this patch is coming from and why we need it, however I'm not sure it's the right approach. We've already introduced "#ahah['callback']" (see #360128: Security fix for simplified AHAH callbacks), so having something called "success_callback" could be confusing when one of them refers to a JavaScript callback and the other one refers to a PHP callback.

Since this patch is intended to provide functionality only in the JavaScript layer, it'd make sense to make the solution purely JavaScript based. We've already setup AHAH objects, but unfortunately they're not actually available anywhere.

This small patch simply makes the AHAH object public, so you can override any of the methods (such as beforeSubmit, success, and error).

An example for a button with the ID "edit-more":

Drupal.ahah['edit-more'].success = function(response, status) {
  alert('Custom callback executed.');
};
BoarK’s picture

To keep with spirit of sticking to the domain of javascript, I added the following to ahah.js after applying patch #2.

Add this code in Drupal.ahah.prototype.success

  if (new_content.parents('html').length > 0) {
    Drupal.attachBehaviors(new_content);
  }
+ if (typeof this.success_callback == "function") {
+  this.success_callback(response, status);
+ }
  Drupal.unfreezeHeight();  

This allows for:

Drupal.ahah['edit-submit'].success_callback = function(response, status) {
  // code goes here.		  
};

without by-passing the default ahah success callback.

apaderno’s picture

Title: support custom success callbacks in ahah.js » Support custom success callbacks in ahah.js
Frando’s picture

FileSize
3.3 KB

Here is a new version of the patch that is based on quicksketch's suggestion.

It is indeed that simple, by making the ahah objects publicly accesible it is possible to override all handler functions (success, error, ..).

I added some documentation, and splitted the success function in two parts, one that handles general success processing (removing the throbber, restoring the form properties) and a new function, Drupal.ahah.insertNewContent that actually inserts the new content into the page. This makes it possible to override just the part where the new content is inserted, which will most likely the most common use case, and so you don't have to copy paste the general success processing code here.

But basically, this patch just adds two lines of code to make the ahah objects public, adds some documentation and moves some code a little bit around. No other changes.

These *very* simple changes add a lot more flexibility to ahah.js and will make it usable in quite a few more situations than it is currently.

Status: Needs review » Needs work

The last submitted patch failed testing.

Frando’s picture

Status: Needs work » Needs review
FileSize
3.3 KB

Rerolled for changes in HEAD.

Status: Needs review » Needs work

The last submitted patch failed testing.

liquixis’s picture

subscribing

stBorchert’s picture

Status: Needs work » Needs review
FileSize
3.18 KB

Re-roll against HEAD (actually against the latest dev release, because I've no access to cvs from here).

stBorchert’s picture

FileSize
3.19 KB

Ah, diffed from wrong directory. No it should pass.

Status: Needs review » Needs work

The last submitted patch failed testing.

stBorchert’s picture

Status: Needs work » Needs review
FileSize
3.41 KB

Ok, now for real.

quicksketch’s picture

Status: Needs review » Reviewed & tested by the community

Patch doesn't break any functionality and adds a lot of needed flexibility. We're doing this same approach in Drupal.tableDrag[keyName], and it's been working well for the Drupal 6 release cycle. I applied the patch and tested the following:

- Table drag
- CCK "more" button
- Poll "more" button
- Resizeable textareas

This patch doesn't change any functional code, it just moves it to a separate function.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

quicksketch’s picture

Awesome! Thanks Dries.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

bmhaskar’s picture

Version: 7.x-dev » 6.19
Assigned: Frando » Unassigned
Category: feature » bug
Priority: Normal » Major
Status: Closed (fixed) » Active

@Dries: Is this patch available in version 6.19?
I downloaded Drupal's 6.19 version from drupal.org yet I had to apply this patch.

supadits’s picture

Version: 6.19 » 6.20

apply #2 and #3 work nice on 6.20

jozzhart’s picture

Status: Active » Needs review

#13: 396466_ahah_public-13.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 396466_ahah_public-13.patch, failed testing.

jozzhart’s picture

Status: Needs work » Needs review

#2: drupal_ahah_objects.patch queued for re-testing.

charlie-s’s picture

bmhaskar changed this to 6.20 as a bug report. Spent a bit of time trying to get this working on 6.x before I realized this is intended for 7. Can someone confirm and update the status / close this thing?

nod_’s picture

Status: Needs review » Fixed

Well, that's not going to make it to 6 and the functionality is already available in 7.x

I'll just mark it fixed to let people read it just in case.

tema_u’s picture

Status: Fixed » Needs review

ahah_success_callbacks_frh.patch queued for re-testing.

ionmedia’s picture

Category: bug » support

i have a form with cck filefield and i want to trigger ajaxtrigger after file was successfully uploaded to server.

all what i need is event in my js, how i can do it without changing ahah.js ?

cweagans’s picture

Priority: Major » Normal

Support requests are neither major nor critical.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.