Drupal Ajax 缓存

发布于 2024-12-05 05:45:10 字数 760 浏览 0 评论 0原文

我正在更新我从 D6 编写的模块到 D7,因此必须在表单中用新的 #ajax 交换旧的 AHAH。

我正在执行一个 ajax 请求,该请求创建一个结果并用它替换一个表单元素。这基本上工作正常,但在第一个请求之后,结果被缓存并且不考虑表单中的更改。我认为这可能是浏览器问题,但 Drupal 是否会发送一个过期标头,导致浏览器采用缓存版本?还有其他想法吗?

hook_cache() 中的片段:

  $form['fieldset']['mybutton'] = array(
    '#type' => 'button',   
    '#value' => t('Send request'),
    '#ajax' => array(
          'callback' => 'mycallback',
          'wrapper' => 'mywrapper',
          'method' => 'replace',
          'effect' => 'fade',  
    )

回调的片段:

function mycallback($form, $form_state) {

        [..]

        $form['fieldset']['mywrapper']['#markup'] = 'test';
        return $form['fieldset']['mywrapper']['#markup'];
    }

I'm updating a module I've written from D6 to D7, and therefore have to exchange the old AHAH by the new #ajax in forms.

I'm performing an ajax request which creates a result and replaces a form element with it. This basically works fine, but after the first request, the result is cached and does not take changes in the form into account. I think this is probably a browser-issue, but could it be that Drupal sends an expiration header which induces the browser to take the cached version? Ot any other ideas?

The fragment in hook_cache():

  $form['fieldset']['mybutton'] = array(
    '#type' => 'button',   
    '#value' => t('Send request'),
    '#ajax' => array(
          'callback' => 'mycallback',
          'wrapper' => 'mywrapper',
          'method' => 'replace',
          'effect' => 'fade',  
    )

A snippet of the callback:

function mycallback($form, $form_state) {

        [..]

        $form['fieldset']['mywrapper']['#markup'] = 'test';
        return $form['fieldset']['mywrapper']['#markup'];
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻身的咸鱼 2024-12-12 05:45:10

我已经遇到过这个问题好几次了,这不是缓存问题。问题是你最初会有一个

包裹在你的 mywrapper 元素中,但是在你的 ajax 回调中你用一个字符串替换它......因此,

包装器被替换,下次按下按钮时,脚本找不到需要替换的

,因为它不再存在了!

此外,mycallback 函数的参数需要通过引用传递,因此请将签名更改为:function mycallback(&$form, &$form_state) {

尝试让您的代码看起来更像这样:

function mymodule_my_form($form, &$form_state) {
  $form['fieldset'] = array(
    '#type' => 'fieldset'
  );

  $form['fieldset']['my_element'] = array(
    '#markup' => 'Some initial markup',
    '#prefix' => '<div id="mywrapper">',
    '#suffix' => '</div>'
  );

  $form['fieldset']['mybutton'] = array(
    '#type' => 'button',   
    '#value' => t('Send request'),
    '#ajax' => array(
      'callback' => 'mymodule_mycallback',
      'wrapper' => 'mywrapper',
      'method' => 'replace',
      'effect' => 'fade',  
    )
  );

  return $form;
}


function mymodule_mycallback(&$form, &$form_state) {
  $form['fieldset']['my_element']['#markup'] = 'New Markup';

  // Always, always, always return an element here, not a string.
  // This makes sure the form state stays consistent.
  return $form['fieldset']['my_element'];
}

如果有疑问,请查看示例模块 ,特别是 ajax_example 模块中的 ajax_example_submit_driven_ajax() 示例。

I've run into this problem a few times and it isn't a caching issue. The problem is you'll originally have a <div> wrapped around your mywrapper element, but in your ajax callback you're replacing it with a string...the <div> wrapper is therefore replace and next time you press the button the script can't find the <div> it needs to replace as it's not there any more!

Also, the arguments for your mycallback function need to be passed in by reference so change the signature to this: function mycallback(&$form, &$form_state) {.

Try making your code look a bit more like this:

function mymodule_my_form($form, &$form_state) {
  $form['fieldset'] = array(
    '#type' => 'fieldset'
  );

  $form['fieldset']['my_element'] = array(
    '#markup' => 'Some initial markup',
    '#prefix' => '<div id="mywrapper">',
    '#suffix' => '</div>'
  );

  $form['fieldset']['mybutton'] = array(
    '#type' => 'button',   
    '#value' => t('Send request'),
    '#ajax' => array(
      'callback' => 'mymodule_mycallback',
      'wrapper' => 'mywrapper',
      'method' => 'replace',
      'effect' => 'fade',  
    )
  );

  return $form;
}


function mymodule_mycallback(&$form, &$form_state) {
  $form['fieldset']['my_element']['#markup'] = 'New Markup';

  // Always, always, always return an element here, not a string.
  // This makes sure the form state stays consistent.
  return $form['fieldset']['my_element'];
}

If in doubt have a look at the examples module, specifically the ajax_example_submit_driven_ajax() example in the ajax_example module.

中二柚 2024-12-12 05:45:10

在第一个答案的帮助下找到了

答案再次添加包装器

function mymodule_mycallback(&$form, &$form_state) {
  $form['fieldset']['my_element']['#prefix'] = '<div id="mywrapper">';
  $form['fieldset']['my_element']['#suffix'] = '</div>';

 // Always, always, always return an element here, not a string.
 // This makes sure the form state stays consistent.
 return $form['fieldset']['my_element'];
}

我不认为你必须使用指针(&)至少drupal示例模块不需要

Figured it out with help of the first answer

Add wrapper again

function mymodule_mycallback(&$form, &$form_state) {
  $form['fieldset']['my_element']['#prefix'] = '<div id="mywrapper">';
  $form['fieldset']['my_element']['#suffix'] = '</div>';

 // Always, always, always return an element here, not a string.
 // This makes sure the form state stays consistent.
 return $form['fieldset']['my_element'];
}

And i don't think that u have to use the pointers (&) at least the drupal example module doesn't

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文