2012-04-08 1 views
0

に影響しない、私はDrupal.attachBehaviorsが、私はDrupalの7に取り組んでいる返された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; 
} 

新しいコメントを追加するには、この

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コールバック関数のように構築され、フォームを持っていますここにdiv(ajax)に新しい作成されたコメントをロードする私のJavascriptがあります

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) 
} 

フックメニューバック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)); 

内でreturnビューの内容は、Javascriptを、他のすべての効果でDrupal.attachBehaviorsで、良いレンダリング返されたビューコンテンツでもうまくいきます。フォーム提出ajaxを除きます。

助けてもらえますか? attachBehaviorsはreturn ajaxフォームでは動作しません。

ありがとうございます!

答えて

0
Drupal.attachBehaviors(context); 

は、基本的

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

によって定義された関数を再実行し、これらの方法は、セレクタ[すでに処理クラス(かバインドをテストする)を追加する必要があります。 [(function(e){})をクリックします。またはそれぞれ(function(){});または何かが既に追加されています。 「コンテクスト」は、新しいコンテンツがオリジナルのビヘイビア関数によってまだ見つかる小さなコンテキストの中にあることがわかっている場合、「ドキュメント」よりも少ないものを渡すことです:この例では、私の親コンテナセレクタに渡すことができます新しい 'div.someSelectorclass'

Drupal.attachBehaviors('div.parentContainerClass'); 

代わりの

Drupal.attachBehaviors(document); 
関連する問題