简体   繁体   English

通过hook_menu调用Drupal 7 AJAX

[英]Drupal 7 AJAX Call via hook_menu

So, I am trying to get my new pane on the Ubercart Checkout page to update whenever someone chooses a new shipping option. 因此,无论何时有人选择新的送货方式,我都希望在Ubercart Checkout页面上更新我的新窗格。 So far I can get this to occur every time I refresh the page after choosing a new shipping option. 到目前为止,选择新的送货方式后,每次刷新页面时,我都可以使这种情况发生。 The next step is to get it working VIA AJAX. 下一步是通过AJAX使它运行。

My problem is that the ajax commands are not firing from the AJAX Callback. 我的问题是ajax命令没有从AJAX回调中触发。 The div is not being updated. div未更新。 Right now I just have some simple text. 现在我只有一些简单的文字。 Later, I will add the actual form information that I need, which will be a whole other issue. 稍后,我将添加所需的实际表单信息,这将是另一个问题。 However, I can't even get this test case to work. 但是,我什至无法使这个测试用例正常工作。

I am working on a new feature for the Ubercart FedEx Module for Drupal. 我正在为Drupal的Ubercart FedEx模块开发一项新功能。 I have been working on this for some time now to no avail. 我已经为此工作了一段时间,但无济于事。 I have tried MANY different things, none work. 我尝试了很多不同的东西,没有用。

To be very clear... I KNOW the ajax call is firing. 非常清楚...我知道ajax调用正在触发。 I can even attach a success to the ajax $.get call and get a console output and the drupal variable is set. 我什至可以将成功附加到ajax $ .get调用中并获取控制台输出,并设置drupal变量。 ONLY the div replace code is not working. 仅div替换代码不起作用。 All other pieces to this puzzle work. 此难题的所有其他作品。

I get to the ajax call and return to the javascript. 我进入ajax调用并返回javascript。 I can even attach a success function on the ajax call in the JS and get console output on success. 我什至可以在JS的ajax调用上附加一个成功函数,并在成功时获取控制台输出。 The javascript is not the issue. javascript不是问题。

// Implements hook_menu()
function uc_fedex_menu() {
  $items = array();
  $items['uc_fedex_jquery_callback/%'] = array(
    'title' => 'My Custom Callback', 
    'description' => 'Listing of blogs.', 
    'page callback' => 'uc_fedex_calendar_jquery_callback', 
    'page arguments' => array(1),
    'access arguments' => array('access content'), 
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

// AJAX callback: Updates calendar
// THIS IS WHERE THE ERROR IS... the variable is set
// But the div content is never replaced.
function uc_fedex_calendar_jquery_callback($argument) {
  variable_set('uc_fedex_shipment_type',$argument);
  $commands[] = ajax_command_replace('#fromdate', "works");
  return array('#type' => 'ajax', '#commands' => $commands);
}

// Actual UI of Calendar Pane
function uc_fedex_uc_checkout_pane_shipdate($op, $order, $form = NULL, &$form_state = NULL) {
    switch ($op) {
    case 'view':
      // Check for shipping quote option without altering Ubercart Core.
      // The $.get line makes the hook_menu call which in turn
      // makes the call back to the above function that has the issue
      drupal_add_js("
      jQuery(function ($) {
      $(document).ready(function() {
      last = 'o';
          setInterval(function() {
           $('#quote input:radio:checked').each(function() {
              if($(this).val() != last){
                last = $(this).val()
                $.get('https://www.fdanconia.com/uc_fedex_jquery_callback/'+ $(this).val());
              }
           });
          }, 500);
        });
      });", 'inline');

      $contents['calendar'] = array(
        '#type' => 'textfield',
        '#title' => t('Choose Your Estimated Arrival Date'),
        '#default_value' => date('m/j/Y',$response[1]),
        '#prefix' => '<div id="fromdate">',
        '#suffix' => '</div>',
      );
      return array('description' => $description, 'contents' => $contents);
    }
}

// Implements hook_uc_checkout_pane().
function uc_fedex_uc_checkout_pane() {
  $panes['calendar'] = array(
    'callback' => 'uc_fedex_uc_checkout_pane_shipdate',
    'title' => t('Shipping Calendar'),
    'desc' => t('A calendar to allow customers to choose shipping date.'),
    'process' => TRUE,
  );
  return $panes;
}

A note about the js, jQuery( function same_func(){} ) is the equivalent of $(document).ready( function same_func() {} ) 关于js的注释jQuery(function same_func(){})等效于$(document).ready(function same_func(){})

So the outer jQuery() calls binds a function to document ready. 因此,外部jQuery()调用将一个函数绑定到文档准备就绪。 The function fires at document ready, and bind another function to document ready, which already triggered. 该函数在文档就绪时触发,并将另一个函数绑定到已触发的文档就绪。

Most of the time in Drupal the variable $ is left unattached so you have to explicitly create a method(function) that passes in jQuery as $. 在Drupal中,大多数时候变量$都是不附加的,因此您必须显式创建一个以$形式传入jQuery的方法(函数)。

(function($){ 

$ available in here

})(jQuery);

Think of the the above as: 
(function)(variables) where the function takes a variable $ .. 

Or you can think of it like this:

function test ($) {
  $(jquery magic).etc()'
}
test(jQuery);

.. except the function test, and the function call are included on the 'same' line. ..除了功能测试外,功能调用都包含在“相同”行中。

Check firebug/console to see if the $.get is being called. 检查萤火虫/控制台,查看是否正在调用$ .get。

Another debug is to use php's error_log('message') in uc_fedex_calendar_jquery_callback() and watch the apache error.log. 另一个调试是在uc_fedex_calendar_jquery_callback()中使用php的error_log('message')并查看Apache error.log。

I ended up adding the parameters I need in the response callback and then processing them on the AJAX response from the original call. 我最终在响应回调中添加了我需要的参数,然后在原始调用的AJAX响应中处理它们。 I just manipulated the DOM using the updated variable data manually with jQuery. 我只是使用jQuery手动使用更新的变量数据来操作DOM。 I do not think there is a way to do this otherwise without altering Ubercart core, which is unacceptable. 我认为没有其他方法可以不改变Ubercart核心,这是不可接受的。

If anyone wants the code I used to fix this, just ask and thou shal receive. 如果有人想要我用来解决此问题的代码,只需询问一下,您就可以收到。 (It is kinda long, but I will post here if someone wants it.) (这很长,但是如果有人愿意,我会在这里发布。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM