AJAX Loading of any Block

Ajax调用任何区块的例子

More complicated way

Place 2 div's with the ID of boost-stats & boost-user where you would like the data to appear. I use a block to do this.

<div id="boost-stats"></div>
<div id="boost-user"></div>

Javascript
drupal_add_js($js_code, 'inline', 'footer');

$.getJSON(Drupal.settings.basePath + "$ajax_menu_callback", {nocache: "1"}, function(response) {
  $.each(response, function(id, contents) {
    if (contents == 'NULL') {
      $(id).parent().parent().hide();
    }
    else {
      $(id).html(contents);
    }
  });
});

PHP

<?php
 
include_once './includes/bootstrap.inc';
 
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

 
// Return Data
 
$json = array();
 
// Get stats block html.
 
$block = module_invoke('statistics', 'block', 'view', 0);
 
$block = $block['content'];
 
$json = array_merge($json, array('#boost-stats' => $block));

 
// Get user block html
 
$block = module_invoke('user', 'block', 'view', 3);
 
$block = $block['content'];
 
$json = array_merge($json, array('#boost-user' => $block));

 
// Send JSON Back
 
if (!empty($json)) {
    echo
json_encode($json);
  }
  exit;

?>

Notes:
Grabbing the info from the block cache is another way to do it; one that doesn't require a full bootstrap.
If JSON returns 'NULL' I have it set to hide that block.
Reason I add nocache=1 to the getJSON request url is because boost will soon be able to cache JSON objects. This tells boost to not cache that url.

文章分类: 
文章标签: