I've tried finding a solution to this at the forums, and failed there, so i thought I would post a support request now.
The forum post is located at http://drupal.org/node/490486.

I'd just like to set a function to be called after Views loads new content using AJAX.

Thanks

Comments

mcrittenden’s picture

You ever figure this out? I'm working on the same thing.

mcrittenden’s picture

Title: Creating a callback for the next page link with pagination. » Creating a callback for AJAX views pagination
Component: Miscellaneous » Code
Status: Active » Fixed

Turns out the easiest (and probably least recommended way) is just to copy the entire function that you're working with out of the Views folder and into your theme's folder, and add your own custom callbacks there. For anybody who thinks that's kind of shady, it's also relatively easy to add callbacks anytime any ajax event is fired, even from outside that ajax event's function. More here: http://docs.jquery.com/Ajax_Events

vamega’s picture

Status: Fixed » Active

I looked at the files in the theme folder of views, and I couldn't find out where I would have to add the custom callback code.
I think the closest I got was in the file views-view.tpl.php where I saw the line

print $pager;

Is this where I would add my callback. And if this is the place. How do I do it?

mcrittenden’s picture

Status: Active » Fixed

vamega: the file is views/js/ajax_view.js ...copy the Drupal.behaviors.ViewsAjaxView there and paste into a JS file inside your theme, and then add your callbacks right after success: function(response) { (which is at line 82 I believe in the current ajax_view.js)

Status: Fixed » Closed (fixed)

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

sepla’s picture

Simply append a $(document).trigger() to the success function in the views/js/ajax_view.js and implement the callback using $(document).bind().

joanpc’s picture

You just have to make a custom module

function yourmodule_ajax_data_alter($object) {
    $object->__callbacks[] = 'Drupal.yourmodule';
}

And add the callback in your .js file:

Drupal.yourmodule = function(target, response) { alert ('hi there!');}




I just realized that if you define your js function on the Drupal.behaviors var it also get called after views ajax calls:

Drupal.behaviors.yourtheme = function () {alert ('hi again!');}
without custom module! :)

nicknickoli’s picture

Seems like the easiest solution is

(function($) {
	$(document).ajaxComplete(function() {
		alert('hi'); // fired after the AHAH is completed 
	});
}(jQuery));

After
http://www.jaypan.com/tutorial/calling-function-after-ahah-event-drupal-...

B Leg’s picture

There's no documentation for "ajax_data_alter" anywhere. Did this ever exist?

NicoDruif’s picture

Thanks, I encountered this same problem, and used the quick and dirty solution posted by nicknickoli. I use $(document).ajaxComplete(function()) function for a callback which attaches the javascript I want. Works like a charm!

cyper983’s picture

Hi, solution #8 worked grat for me!!!

Thank you nicknickoli!!

justindodge’s picture

Note that the solution proposed in #8 will execute that javascript for all ajaxComplete events, not just the one you may be looking for.

psegarel’s picture

Issue summary: View changes

#8 should work but it would be better to restrict the callback to a specific event...

From the JQuery docs

All ajaxComplete handlers are invoked, regardless of what Ajax request was completed. If you must differentiate between the requests, use the parameters passed to the handler. Each time an ajaxComplete handler is executed, it is passed the event object, the XMLHttpRequest object, and the settings object that was used in the creation of the request. For example, you can restrict the callback to only handling events dealing with a particular URL:

$( document ).ajaxComplete(function( event, xhr, settings ) {
  if ( settings.url === "ajax/test.html" ) {
    $( ".log" ).text( "Triggered ajaxComplete handler. The result is " +
      xhr.responseText );
  }
});
smurfxx’s picture

#8 does not working anymore with latest version of drupal, I used that method some time ago but now it seems broken.
Can you please tell me how to trigger a custom JS function on ajaxComplete?