drupal ajax 分页,ajax_render

这段代码展示了 Drupal 系统中如何处理 AJAX 响应,确保只加载页面未包含的 CSS 和 JS 文件。通过 drupal_get_css 和 drupal_get_js 函数获取当前请求所需的文件,并对比已加载的文件,避免重复加载。同时,代码还处理了 Drupal 设置的更新,允许模块修改 AJAX 响应。
摘要由CSDN通过智能技术生成

function $commands = array()) {

// Although ajax_deliver() does this, some contributed and custom modules

// render Ajax responses without using that delivery callback.

// Ajax responses aren't rendered with html.tpl.php, so we have to call

// drupal_get_css() and drupal_get_js() here, in order to have new files added

// during this request to be loaded by the page. We only want to send back

// files that the page hasn't already loaded, so we implement simple diffing

// logic using array_diff_key().

foreach (array(

'css',

'js',

) as $type) {

// It is highly suspicious if $_POST['ajax_page_state'][$type] is empty,

// since the base page ought to have at least one JS file and one CSS file

// loaded. It probably indicates an error, and rather than making the page

// reload all of the files, instead we return no new files.

if (empty($_POST['ajax_page_state'][$type])) {

$items[$type] = array();

}

else {

$function = 'drupal_add_' . $type;

$items[$type] = $function();

$type, $items[$type]);

// @todo Inline CSS and JS items are indexed numerically. These can't be

// reliably diffed with array_diff_key(), since the number can change

// due to factors unrelated to the inline content, so for now, we strip

// the inline items from Ajax responses, and can add support for them

// when drupal_add_css() and drupal_add_js() are changed to use a hash

// of the inline content as the array key.

foreach ($items[$type] as $key => $item) {

if ($key)) {

unset($items[$type][$key]);

}

}

// Ensure that the page doesn't reload what it already has.

$items[$type] = $items[$type], $_POST['ajax_page_state'][$type]);

}

}

// Render the HTML to load these files, and add AJAX commands to insert this

// HTML in the page. We pass TRUE as the $skip_alter argument to prevent the

// data from being altered again, as we already altered it above. Settings are

// handled separately, afterwards.

if (isset($items['js']['settings'])) {

unset($items['js']['settings']);

}

$styles = $items['css'], TRUE);

$scripts_footer = 'footer', $items['js'], TRUE);

$scripts_header = 'header', $items['js'], TRUE);

$extra_commands = array();

if (!empty($styles)) {

$extra_commands[] = $styles);

}

if (!empty($scripts_header)) {

$extra_commands[] = 'head', $scripts_header);

}

if (!empty($scripts_footer)) {

$extra_commands[] = 'body', $scripts_footer);

}

if (!empty($extra_commands)) {

$commands = $extra_commands, $commands);

}

// Now add a command to merge changes and additions to Drupal.settings.

$scripts = 'js', $scripts);

if (!empty($scripts['settings'])) {

$settings = $scripts['settings'];

$commands, $settings['data']), TRUE));

}

// Allow modules to alter any Ajax response.

$commands);

return $commands);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值