Posted by Merlin on Fri, 11/12/2010 - 12:54pm
These are my slides and example module for my BADCamp presentation tomorrow.
Drupal 7 advanced ajax
View more presentations from merlinofchaos.
| Attachment | Size |
|---|---|
| 3.24 KB |

Comments
kinda like slide 12
Inspired by your examples but still can't do what I want to do.
Hi Earl,
Thank you for these great examples. It helps a lot !
But as I said in the title, I still can't reach my goal.
What I would like to achieve:
When a user clicks on the ajax link, I don't want the ajax stuff to be triggered right away.
I want to process several tasks before:
- remove some html tags
- create my own throbber and place it in a very specific div...
- then trigger the ajax stuff
I tried to write some js based on your example where you create a custom event and trigger it later in the code.
My code works correctly except for the ajax stuff: at the end, the browser is redirected to the nojs page.
I can't figure out what is wrong in my code:
Drupal.behaviors.CTAclickbutton = {
attach: function (context, settings) {
/**
* Use a class to bind custom behavior to a link. As it happens, the behavior
* if this link is nearly exactly the same as with "use-ajax". But instead
* we are ensuring that the throbber is active.
*/
$('.ctabutton-use-ajax:not(.ajax-processed)').addClass('ajax-processed').each(function () {
var element_settings = {};
// Remove drupal throbber.
element_settings.progress = {};
// For anchor tags, these will go to the target of the anchor rather
// than the usual location.
if ($(this).attr('href')) {
element_settings.url = $(this).attr('href');
element_settings.event = 'DelayedClick';
}
var base = $(this).attr('id');
var ajax = new Drupal.ajax(base, this, element_settings);
$(this).click(function () {
$('#slidesHolder').remove();
$(ajax.element).trigger('DelayedClick');
Drupal.ajax[base] = ajax;
});
});
}
};
Any help would be really appreciated.
Cheers
Geraud
Update
Almost there !
I added a "return false" in order to avoid the redirection to /nojs url.
But the ajax stuff is not triggered (no POST in the firebug console).
Here is the new code:
// Click on CTA button
Drupal.behaviors.CTAclickbutton = {
attach: function (context, settings) {
/**
* Use a class to bind custom behavior to a link. As it happens, the behavior
* if this link is nearly exactly the same as with "use-ajax". But instead
* we are ensuring that the throbber is active.
*/
$('.ctabutton-use-ajax:not(.ajax-processed)').addClass('ajax-processed').each(function () {
var element_settings = {};
// Remove drupal throbber.
element_settings.progress = {};
// For anchor tags, these will go to the target of the anchor rather
// than the usual location.
if ($(this).attr('href')) {
element_settings.url = $(this).attr('href');
element_settings.event = 'DelayedClick';
}
var base = $(this).attr('id');
var ajax = new Drupal.ajax(base, this, element_settings);
$(this).click(function () {
$('#slidesHolder').remove();
$(ajax.element).trigger('DelayedClick');
return false;
});
Drupal.ajax[base] = ajax;
});
}
};
Thanks!
I've been looking for sample code like this for months. Thank you!
Found it !
My mistake:
$('#slidesHolder').remove();
Trying to remove the div that holds the ajax button before triggering the ajax stuff is a bad thing...
(king of stupid too).
I replaced it with $('#slidesHolder').fadeOut() and it works.
Thanks again for your great examples and sorry for polluting your post with my pbs.
Got it working, but....
Firstly, this is just the code I've been looking for. I agree, I've found plain Drupal Ajax too constrained.
Now for the 'but'; I want to pass a user entered parameter to the ajax method. Setting it in the js method isn't working because it appears the method is already linked to the button (ajax is executed with the value not set). How should I approach this issue within this framework?
Thanks...
This is hard. You have to
This is hard. You have to replace the trigger callback method or the beforesend method with a custom one that can adjust the parameters.
Alter the beforeSubmit method
Hello,
After reading kevinmanx's post and your answer, I found a solution for a similar problem.
In my case, I must add some url arguments before the ajax call. But if the ajax method is already declared, I was blocked and any try to add the arguments was not possible.
// add behavior when user try to add new action
$('#action-add:not(.ajax-processed)').addClass('ajax-processed').each( function() {
var base = $(this).attr('id');
var element_settings = {};
element_settings.event = 'DelayedClick';
element_settings.url = "/js/mymodule/add_action/";
var ajax = new Drupal.ajax( base, this, element_settings );
$(this).click(function() {
name = $('#edit-instance-settings-mymodule-action-name').val();
if( name.length ) {
// tried the following :
element_settings.url += name;
ajax.element_settings.url += name;
Drupal.ajax[base].element_settings.url += name;
$(ajax.element).trigger('DelayedClick');
return false;
}
else {
alert( "You must add a name before adding action" );
}
});
Drupal.ajax[base] = ajax;
});
Your answer put me on the right way (I hope...) : override the beforeSubmit method of ajax object :
// add behavior when user try to add new action
$('#action-add:not(.ajax-processed)').addClass('ajax-processed').each( function() {
var url = "/js/mymodule/add_action/";
var base = $(this).attr('id');
var element_settings = {};
element_settings.event = 'DelayedClick';
element_settings.url = "";
var ajax = new Drupal.ajax( base, this, element_settings );
$(this).click(function() {
name = $('#edit-instance-settings-mymodule-action-name').val();
if( name.length ) {
Drupal.ajax.prototype.beforeSubmit = function (form_values, element, options) {
options.url = url + name;
}
$(ajax.element).trigger('DelayedClick');
return false;
}
else {
alert( "You must add a name before adding action" );
}
});
Drupal.ajax[base] = ajax;
});
I don't know if it's the correct way to alter the element_settings object but it work for me. Maybe you can correct me if I'm wrong (ie alter prototype method break other ajax calls) and give us the right approach :-)
Add new comment