17

I have a map which loads markers via ajax on various/numerous map events. Currently, I define a custom page callback for my hook_menu() item which prints my json/js.

Since my page callback does not return any content and only prints content, Drupal does not render the full template, menu, etc, and thus allows me to return a page with only my JavaScript.

Although this does work, I believe that Drupal still fully bootstrapping, which means it is adding a lot of overhead I would like to drop.

I've been looking at High-performance JavaScript callback handler, which boasts itself as a "High-performance JavaScript callback handler" and which, in short, looks as though it offers a js.php to compliment index.php to direct your requests to and customize/limit/optimize the bootstrapping Drupal does by default.

Unfortunately the js project doesn't have a D7 release yet (petition for one here), which leads us to the beginning...

What is the fastest way to implement an Ajax callback in Drupal?

8

I define a custom page callback for my hook_menu() item which prints my json/js.

That is what Drupal 6 offered. The Drupal 7 AJAX framework can do a little more than that. Yes it bootstraps Drupal, but it also gives a lot of features for free. In short you can use ajax_deliver() as a page delivery method, and send commands to the browser.

... Ajax may be enabled by adding classes to buttons and links. By adding the 'use-ajax' class to a link, the link will be loaded via an Ajax call. When using this method, the href of the link can contain '/nojs/' as part of the path. When the Ajax framework makes the request, it will convert this to '/ajax/'. The server is then able to easily tell if this request was made through an actual Ajax request or in a degraded state, and respond appropriately.

However if you want to sidestep the framework for performance reasons then there is nothing to stop you calling php files in your module directly from javascript. You will get no Drupal goodness but you also will have complete control over the page.

  • Thanks! Regardless of the performance I really should be using the Ajax Framework (now that I know about it) b/c any improvements to the ajax page request process will likely happen within that context :) – electblake Apr 8 '11 at 15:58
  • 2
    Have a look at the example modules, they will help a lot, the framework is geared towards forms but can be used anywhere. I have edited my answer with a bit about the 'use-ajax' class. – Jeremy French Apr 8 '11 at 16:01
  • 3
    Calling PHP files directly is fine sometimes. You can even have that file include the bootstrap.inc and then call api.drupal.org/api/drupal/includes--bootstrap.inc/function/… to the specific level you need it. – greggles Apr 12 '11 at 20:26
4

A bit late, but especially if you are using that ajax callback for polling (check for new messages for example), there is an interesting alternative: Drupal node.js integration.

Instead of polling, it allows you to build an event-like system, where users (aka the JS on their page) keep an open connection to your node.js server. If something happens, you can directly notify all users through node.js.

  • Never too late for great suggestions - never even thought of using node.js for this sort of thing but suddenly it makes sense heh, thanks! – electblake May 20 '11 at 16:20
3

Since you want to implement an Ajax callback in Drupal, using the JS module looks like a good bet. I just took a quick look at the code, and it's basically what I would've suggested (but with a lot more attention to details). The maintainer is one of the top Drupal developers, too.

It looks like you'll have to use module_load_include() or perhaps a more manual include 'sites/all/modules/contrib/...'; approach if you need to use APIs from other modules.

The very problem you're talking about has been a hot topic lately. Dries's keynote from DrupalCon Chicago talked about how Drupal 8 needs better APIs for doing callbacks and non-HTML output formats.

  • So, in short - keep on researching JS.php & d.o/project/js ? I'll start petitioning in the issue que for D7 release. Thanks for the video! – electblake Apr 7 '11 at 21:19
  • Yeah its a real shame the JS project hasn't been ported to D7 yet. Its a shame its so hard to get a lightweight page request. – Matt Collins Feb 16 '12 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.