2012-04-08 53 views
0

我工作的Drupal 7的,我有一個表格構建這樣Drupal.attachBehaviors不會影響到返回阿賈克斯形式

function qt_debate_response_form($form, &$form_state, $node_id){ 
$form['node_id'] = array(
    '#type' => 'value', 
    '#value' => $node_id, 
); 
$form['response_body'] = array(
    '#type' => 'textarea', 
    '#required' => TRUE, 
    '#row' => 4, 
    '#default_value' => '', 
); 
$form['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Post'), 
    '#ajax' => array(
     'callback' => 'qt_debate_response_form_js', 
     'wrapper' => 'response-message-' . $node_id, 
     'method' => 'append', 
     'effect' => 'fade', 
    ), 
); 
return $form; 
} 

而一個AJAX回調函數來添加新評論

function qt_debate_response_form_js($form, $form_state) { 
global $user; 
$body_text = $form_state['values']['response_body']; 
$node_id = $form_state['values']['node_id']; 

$message_js = ' 
<script language="javascript" type="text/javascript"> 
    qt_debate_response_load_new_item(' . $node_id . ',' . $user->uid . '); 

    jQuery(".response-form-wrapper textarea").val(""); 
</script>'; 


$comment = new stdClass(); 
$comment->nid = $form_state['values']['node_id']; // Node Id the comment will attached to 
$comment->cid = 0; 
$comment->pid = 0; 
$comment->uid = $user->uid; 
$comment->is_anonymous = 0; 
$comment->homepage = ''; 
$comment->status = COMMENT_PUBLISHED; 
$comment->language = LANGUAGE_NONE; 
$comment->subject = text_summary($body_text, null, 60); 
$comment->comment_body[$comment->language][0]['value'] = $body_text; 
$comment->comment_body[$comment->language][0]['format'] = 'filtered_html'; 
comment_submit($comment); 
comment_save($comment); 

$output = $message_js; 

return $output; 
} 

這裏是我的Javascript,加載新創建的註釋到Div(ajax)

function qt_debate_user_post_load_new_items(debate_id) { 
// get the latest comment id in context 
$top_comment = jQuery(".view-debate-user-posts .views-row").first(); 
$top_comment_id = jQuery(".nid-field-hidden", $top_comment).html(); 

jQuery.ajax({ 
    type: "GET", 
    url: "/qt_debate/ajax/load_new_items/" + debate_id + "/" + $top_comment_id, 
    data: "", 
    success: function(html){ 
     $new_items = jQuery(".view-content", html); 
     jQuery("form", $new_items).attr("action","/debate/199"); 
     jQuery(".form-submit", $new_items).attr("id","edit-submit--5"); 

     if ($new_items.html() != null) { 
      html = '<div class="new_items_wrapper" style="display: none">' + $new_items.html() + '</div>'; 
      if (jQuery(".view-debate-user-posts .view-content").length == 0) { 
       jQuery(".view-debate-user-posts .view-empty").remove(); 
       jQuery(".view-debate-user-posts").append('<div class="view-content"></div>'); 
      } 

      jQuery(".view-debate-user-posts .view-content").prepend(html); 

      jQuery(".view-debate-user-posts .view-content .new_items_wrapper").fadeIn(500, function() { 
       jQuery(".views-row", this).unwrap(); 
       Drupal.attachBehaviors(); 
      }); 
     } 
    }, 
}); 

var t = setTimeout("qt_debate_user_post_load_new_items(" + debate_id + ")", 30000) 
} 

hook_menu這是轉向jQuery的內容的觀看次數再打

function qt_debate_ajax_load_new_items() { 
$debate_id = arg(3); 

print views_embed_view('debate_user_posts_load_new_items_', 'default', array($debate_id)); 
exit(0); 
} 

查看模板文件,我也內

print drupal_render(drupal_get_form('qt_debate_response_form', $row->nid)); 

返回一個新的形式返回查看內容呈現較好,在Javascript Drupal.attachBehaviors,所有其它影響在返回的視圖內容也工作得很好。除了表單提交ajax。

任何人都可以幫忙嗎? attachBehaviors不適用於返回ajax形式。

非常感謝!

回答

0
Drupal.attachBehaviors(context); 

基本上重新運行由

Drupal.behaviors.yourFunctionName = function(context) { 
    $('div.someSelectorclass:not(.already-processed-class)', context).addClass('already-processed-class').bind(someMethod); 
} 

定義的任何功能和這些方法必須添加一個選擇器[已經處理級]以測試是否綁定(); [或點擊(功能(e){});或每個(function(){});或任何]已被添加。 「context」是傳遞比'document'更少的內容 - 比如說,如果你的新內容被認爲是在一個更小的上下文中,那麼它仍然可以被原始行爲函數找到:在這個例子中,我可以傳遞我的父容器選擇器新的 'div.someSelectorclass'

Drupal.attachBehaviors('div.parentContainerClass'); 

,而不是

Drupal.attachBehaviors(document);