簡體   English   中英

通過hook_menu調用Drupal 7 AJAX

[英]Drupal 7 AJAX Call via hook_menu

因此,無論何時有人選擇新的送貨方式,我都希望在Ubercart Checkout頁面上更新我的新窗格。 到目前為止,選擇新的送貨方式后,每次刷新頁面時,我都可以使這種情況發生。 下一步是通過AJAX使它運行。

我的問題是ajax命令沒有從AJAX回調中觸發。 div未更新。 現在我只有一些簡單的文字。 稍后,我將添加所需的實際表單信息,這將是另一個問題。 但是,我什至無法使這個測試用例正常工作。

我正在為Drupal的Ubercart FedEx模塊開發一項新功能。 我已經為此工作了一段時間,但無濟於事。 我嘗試了很多不同的東西,沒有用。

非常清楚...我知道ajax調用正在觸發。 我什至可以將成功附加到ajax $ .get調用中並獲取控制台輸出,並設置drupal變量。 僅div替換代碼不起作用。 此難題的所有其他作品。

我進入ajax調用並返回javascript。 我什至可以在JS的ajax調用上附加一個成功函數,並在成功時獲取控制台輸出。 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;
}

關於js的注釋jQuery(function same_func(){})等效於$(document).ready(function same_func(){})

因此,外部jQuery()調用將一個函數綁定到文檔准備就緒。 該函數在文檔就緒時觸發,並將另一個函數綁定到已觸發的文檔就緒。

在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);

..除了功能測試外,功能調用都包含在“相同”行中。

檢查螢火蟲/控制台,查看是否正在調用$ .get。

另一個調試是在uc_fedex_calendar_jquery_callback()中使用php的error_log('message')並查看Apache error.log。

我最終在響應回調中添加了我需要的參數,然后在原始調用的AJAX響應中處理它們。 我只是使用jQuery手動使用更新的變量數據來操作DOM。 我認為沒有其他方法可以不改變Ubercart核心,這是不可接受的。

如果有人想要我用來解決此問題的代碼,只需詢問一下,您就可以收到。 (這很長,但是如果有人願意,我會在這里發布。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM