2012-06-07 83 views
1

因此,我試圖讓我的新窗格在Ubercart Checkout頁面上更新,只要有人選擇新的送貨選項。到目前爲止,我可以在選擇新的送貨選項後每次刷新頁面時都會發生這種情況。下一步是讓它工作在威盛AJAX。Drupal 7 AJAX通過hook_menu撥打電話

我的問題是,ajax命令不會從AJAX回調中觸發。該div沒有被更新。現在我只是有一些簡單的文字。之後,我將添加我需要的實際表單信息,這將是另一個問題。但是,我甚至無法讓這個測試案例起作用。

我正在爲Ubercart FedEx Module for Drupal提供一項新功能。我一直在研究這個問題一段時間無濟於事。我嘗試了很多不同的東西,但都沒有成功。

要非常清楚......我知道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; 
} 
+0

你試過用ahah –

回答

0

我最終在響應回調中添加了我需要的參數,然後在來自原始調用的AJAX響應中處理它們。我只是用jQuery手動使用更新後的變量數據操縱DOM。如果不改變Ubercart內核,我認爲沒有辦法做到這一點,這是不可接受的。

如果任何人想要我用來解決這個問題的代碼,只要問你應該接受。 (這是有點長,但我會張貼在這裏,如果有人想要它。)

0

關於JS的說明, 的jQuery(功能same_func(){})是$(文件)。就緒的當量(功能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); 

..除了函數測試和函數調用包含在'同一行'。

檢查Firebug /控制檯以查看是否正在調用$ .get。

另一個調試是在uc_fedex_calendar_jquery_callback()中使用php的error_log('message')並觀察apache error.log。

+0

嗯......你顯然沒看過我的問題。我完全知道如何在Drupal的上下文中使用jQuery,並且執行我的JavaScript代碼時有0個問題。問題在於來自hook_menu的ajax調用。 $ .get被稱爲......正如我在我的問題中明確指出的那樣。 –