0

我在 Drupal 中有一个表单,它调用 Netezza 中的外部数据库。从 Netezza 检索此数据大约需要 10 秒。然后,基于该信息,我必须构建一个选择控件,让用户从类别列表中进行选择。当用户选择一个类别时,我会再次调用 Netezza 以检索更多信息。

问题在于,对于第二次交互(当用户选择一个类别时),表单被重新处理,因此对 Netezza 进行了 2 次昂贵的调用,而不是任何人期望或期望的调用。

您知道这种情况的解决方法吗?有没有办法在不重建整个表单的情况下使用 Drupal Ajax 框架进行 ajax 调用?

谢谢。

PD:阅读有关 Ajax 框架的文档 我想一个解决方案可能是使用另一个指定 #ajax['path'] 的路径,但尚未完全测试该行为,如果您分享您的经验,将不胜感激。

PD2:我更喜欢基于 Drupal Ajax 框架的解决方法,而不是缓存机制。

4

1 回答 1

0

我强烈建议您查看Drupal 示例,特别是名为 ajax_example 的模块。

这是一个快速的示例代码,可能没有运行,只是为了给你一个想法

function expensive_form($form, &$form_state) {

  $form['category'] = array(
    '#title' => t('Cateogry'),
    '#type' => 'select',
    '#options' => first_expensive_operation(),
    '#ajax' => array(
      'callback' => 'choose_category_callback',
      'wrapper' => 'ajax-div',
      // 'method' defaults to replaceWith, but valid values also include
      // append, prepend, before and after.
      // 'method' => 'replaceWith',
      // 'effect' defaults to none. Other valid values are 'fade' and 'slide'.
      'effect' => 'slide',
      // 'speed' defaults to 'slow'. You can also use 'fast'
      // or a number of milliseconds for the animation to last.
      // 'speed' => 'slow',
    ),
  );

  $form['ajax_fieldset'] = array(
    '#title' => t("Ajax Fields"),
    // The prefix/suffix provide the div that we're replacing, named by
    // #ajax['wrapper'] above.
    '#prefix' => '<div id="ajax-div">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#description' => t('This is where we get automatically updated something'),
  );

  // this will only be executed on the second run of the form
  // when the category is set.
  if (isset($form_state['values']['category'])) {
    $form['ajax_fieldset']['something'] = array(
      '#title' => t('Somethings'),
      '#type' => 'select',
      '#options' => second_expensive_operation($form_state['values']['category']),
    );
  }

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}



/**
 * Callback element needs only select the portion of the form to be updated.
 * Since #ajax['callback'] return can be HTML or a renderable 
 * array, we can just return a piece of the form.
 */
function choose_category_callback($form, $form_state) {
  return $form['ajax_fieldset'];
}
于 2014-10-14T20:34:20.433 回答