I've been playing with the new ajax framework and have successfully used it for the admin form on the D7 version of Quicktabs - awesome stuff, the ajax callback is so simple! :-)
Anyway, I was also testing the use of the 'use-ajax' class to do just a regular ajax request when clicking on a link. My menu callback calls ajax_deliver() as long as ajax is passed in and not nojs. I can get this to work only by hacking ajax.js to tell it what element.wrapper and element.click should be, otherwise it causes a js error as these properties are missing and I can't figure out how to set them from the php side. I, perhaps naively, thought that something like this should work:
$build['ajaxy_link'] = array(
'#type' => 'link',
'#title' => t('Click here'),
'#href' => 'ajaxy/nojs/',
'#attributes' => array('class' => array('use-ajax')),
'#ajax' => array(
'event' => 'click',
'wrapper' => 'content',
),
);
Anyway, this does not load ajax.js on the page and does not communicate the wrapper and event properties as required. It would be great if someone who understands how this should work could either add the explanation to the existing docs or give a brief instruction in this thread and I'll write it up in the docs.
Comment | File | Size | Author |
---|---|---|---|
#17 | drupal.ajax_documentation_610068_17.patch | 8.67 KB | katbailey |
#14 | ajax_updated_ajax_inc.zip | 4.93 KB | rfay |
#13 | drupal.ajax_documentation_610068_13.patch | 7.35 KB | rfay |
#7 | ajax_links.patch | 26.83 KB | katbailey |
#6 | ajaxy.tar_.gz | 821 bytes | katbailey |
Comments
Comment #1
Scott Reynolds CreditAttribution: Scott Reynolds commentedsubscribing
Comment #2
rfayExcellent - I plan to do full Ajax docs for the new system and will try to cover this. As of now, I haven't explored it.
Comment #3
rfayI'm not to no-js yet, but
1. Updated the 6-to-7 documentation at http://drupal.org/update/modules/6/7#ahah-now-ajax
2. Committed the D7 version of the Ajax Example to the Examples Module
I would appreciate review of those two, plus contributions of more sophisticated AJAX examples. There's a lot more stuff in here!
The coolest thing in the world is how simple the callbacks have become. One line for each of my examples. I guess we could have figured out how to do away with the callback entirely if you wanted to replace the entire form.
Comment #4
rfayMay I hijack this issue for a general To-Do list for Ajax Documentation? There are several different issues that need to be addressed. I will maintain this comment as time goes on.
D6 to D7 upgrade information (Currently covers what you need to know for existing code, but doesn't cover any AJAX new features.)I'm going to call this one good.General change to 'callback' from 'path' in forms.Change to replacing the entire div instead of just the contents.#599804: Unify page, AJAX 'path', and AJAX 'callback' callbacks made the Ajax callback behave like a page callback.#551694: Standardize the return of status messages in AJAX callbacks - Changed how status messages are handled. Status message handling should be discussed.Good examples. The Examples module now has an Ajax Example module, which shows basic forms use of AJAX. We need to introduce some of the more sophisticated things that are available.the callback from drupal_get_form() changed signature and now takes $form and $form_state as arguments. Mention this, as it's a stumbling block. (Already updated on module upgrade page.Updated the Forms API reference to show current usageHow to handle system messages resulting from AJAX actions. Automatic and manual handling. #551694: Standardize the return of status messages in AJAX callbacksOther items that should be covered? Chime in!
Comment #5
effulgentsia CreditAttribution: effulgentsia commentedFor now, subscribing. When I'm able to, I'd also like to help.
Comment #6
katbailey CreditAttribution: katbailey commentedJust want to add a bit of clarification regarding my initial question above. Rfay and I were discussing this on irc yesterday and the more I look at the code the more I realise that what I was trying to do above is definitely not possible, currently. The only time ajax.js gets automatically added is when ajax_process_form is called. So it seems it really is only intended for forms.
Nobody seems to know how the use-ajax class is supposed to be used currently (or if it can be) so I've been hacking away seeing what it would take to get it to work the way I initially assumed it did. The attached patch allows you to set up ajax functionality on a link (independent of any form) when the link is rendered as follows:
I should have pointed out above that rendering a link this way is only possible with this patch: #602522: Links in renderable arrays and forms (e.g. "Operations") are not alterable
So, I'm attaching a patch which builds on this and, just as a proof of concept, adds an ajax_process_link() function - the first hunk in the patch - which replicates the main functionality in ajax_process_form. I assumed I could add this to the type definiton of the link element in system.module like this:
... but that didn't work so instead I'm calling it from within drupal_pre_render_link.
Anyway, I'd love if someone could look at this and comment, even if it's just to say that these are the ravings of a crazy person ;-)
The attached module should help test it - just enable the one block in a sidebar. This assumes your page has an element with id of 'block-system-main'.
thanks!
K
Edit: I know this is no time for proofs of concept. It's just that there does seem to be a gap in the ajax framework and I'm wondering if this is the way to fill it...
Comment #7
katbailey CreditAttribution: katbailey commentedAttaching an updated patch as there was a problem with the href in the last one...
Comment #8
katbailey CreditAttribution: katbailey commentedOK, finally got this one figured out, after chatting with merlinofchaos in irc - I had misunderstood how this was supposed to work and now it all makes sense. A tiny patch was required though and you'll find it here: #615504: Ajax framework's use-ajax class doesn't work.
And here's an example of how you'd use the use-ajax class:
Comment #9
rfayI confirm that this approach works. Thanks so much for the detective work!
Comment #10
Roi Danton CreditAttribution: Roi Danton commentedThanks for mentioning this issue in IRC (was afk therefore I couldn't reply there). The examples (also the ones in examples module) are helpful when working with ctools wizard ajax render callback (D6), too. There in the ajax-responder.js the click event is attached to links.
Comment #11
te-brian CreditAttribution: te-brian commentedI was playing around with the AJAX framework this morning, and came across a relatively new use case: adding new custom commands.
I wanted to add some commands for working with a jQuery UI Dialog. An example of my approach, which is working but not fully tested, follows:
mymodule.module
mymodule.js
The result is that I can add the 'mymodule_dialog_options' command in my ajax callbacks and my custom command function runs on the client side.
So here is an actual question :)
Is there a better way to add these commands to the Drupal.ajax object(s)? The way I got it working, is adding the command to each Drupal.ajax object that exists one-by-one. The better approach, which I could not get working, would be to add it to all Drupal.ajax objects in one swoop. This is where my javascript mastery falls a bit short. I initialy tried to do this:
But I guess the Drupal.ajax object is not extendable in this way?
Comment #12
rfayCommitted update to forms_api_reference.html updating to current AJAX situation.
http://drupal.org/cvs?commit=294182
Comment #13
rfayThis documentation-only patch adds significant information to the AJAX overview in ajax.inc.
New topics covered or with improved coverage include:
Plus lots of little cleanup.
Comment #14
rfayIt's nearly impossible to review a doc change as a patch, so attached is the HTML rendered by the API module. It doesn't show what's changed, but really, everything is open for improvement.
Comment #15
rfay#13: drupal.ajax_documentation_610068_13.patch queued for re-testing.
Comment #16
katbailey CreditAttribution: katbailey commentedThis is a great enhancement to the ajax docs - it's very helpful to have example code in there.
There's still one part of the docs that I have a problem with:
Maybe I'm missing something, but on the php side, each command is just an associative array and only becomes an object on the js side. So, would something like the following make more sense:
Comment #17
katbailey CreditAttribution: katbailey commentedAttached patch makes the change in #16.
Comment #18
rfayGood by me. Marking RTBC - thanks for the review and improvement.
The bot approved #17 despite the lack of marking on it: See http://qa.drupal.org/pifr/test/26734
For other reviewers, #17 is still exactly the same as #13 (which you can read as HTML in the attachment to #14) - except for the following change.
Comment #19
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.
Comment #20
rfayRemaining items to do in #4
Comment #21
rfayI think that no-js and use-ajax are the last remaining items to be documented in this issue
Comment #22
rfayI'll put this on my list for docs day.
@katbailey, could you write a quick description here of what you think should be said? 'twould be much appreciated.
Comment #23
rfayI'm going to say that http://api.drupal.org/api/group/ajax/7 + the AJAX link example are good enough.
Comment #25
rfayHmm... I don't find anything about class="no-js" *anywhere* in D7 code or documentation. Did I miss something?
Comment #26
ñull CreditAttribution: ñull commentedThis code as proposed by katbaily is unsuitable for dynamic content. I always get the same date/time here (see changed ajax response code above). No effect when I clear the browser cache in between. Only reloading the page where this link is helps. How do I "uncache" it? Would be good to include that too in the examples and documentation.
Comment #27
ñull CreditAttribution: ñull commentedForget my post. For some obscure reason now it works.
Comment #28
nod_Comment #29
LewisNymanComment #36
andypostComment #40
seanBNew data attributes are being added to links with the
use-ajax
class in #3026636: Allow AJAX links to replace a specific selector (CR: https://www.drupal.org/node/3031444).This probably needs to be taken into account when writing the documentation for the AJAX links.
As far as I can tell, the ajax link support the following data attributes:
The content is always being fetched from the href attribute of the link.
#3026636: Allow AJAX links to replace a specific selector adds the following attributes:
Comment #43
paper boyI ran into this old issue but couldn't find any documentation page for use-ajax for Drupal 8. Does this docs page already exist somewhere?
I added a documentation for Ajax Dialogs to the AJAX API Guide that explains how to use the use-ajax class to display Dialogs. If there's no other docs page for use-ajax yet, I'd add a documentation page to the AJAX API Guide that focuses on use-ajax and use-ajax-submit.