2014-01-24 35 views
0

我有一個子窗體,我通過ajax獲取並追加到我的主窗體。在服務器端,我使用'collapsible'和'collapsed'類將dulpal_render數組包裝到fieldset中,然後將它傳遞迴json(SOP)。在前端,我可以使用下面的代碼獲得了可摺疊的行爲:什麼是正確的方式來初始化Drupal崩潰行爲

jQuery('#my-fieldset legend span').click(function(){ 
Drupal.toggleFieldset(jQuery('#my-fieldset')); 
}); 

這工作,但它似乎並不像做正確的方式。我嘗試使用這個碼附加的行爲:

Drupal.behaviors.collapse.attach(jQuery('#my-fieldset'), Drupal.settings); 

Drupal.behaviors.collapse.attach(jQuery('#my-fieldset')); 

但既不正確應用的行爲。

那麼手動初始化這種行爲的正確方法是什麼?

TIA

我想我知道了:

Drupal.behaviors.collapse.attach(jQuery(document), Drupal.settings); 

SOLUTION

感謝prabeen義理! Drupal.attachBehaviors更有意義...

我用drupal.attachBehaviors()測試過,但也許我沒有正確使用它。我認爲我傳遞了一個jQuery對象作爲上下文,而不僅僅是選擇器,正如您提到的示例後所示,如果這有所作爲。我會再玩一些。

我也並不熟悉,這是很好的簡寫我的傳統方法的load()方法...

jQuery.ajax({ 
    url: Drupal.settings.basePath + url, 
    type: 'POST', 
    data: data, 
    dataType: 'json', 
    success: function(response){ 
     if (response.status != undefined && response.data != undefined && response.status == 'ok') { 
     callback(response.data); 
     } 
     return false; 
    } 
    }); 

感謝您的見解。很有幫助。

(我給予好評你,但我沒有REP)

回答

0

Drupal.attachBehaviors()是手動附着在Drupal7

行爲,您可以在這裏看到https://drupal.org/node/1143768參考的方式。

更新

AttachBehaviours被調用任何Ajax請求的完成。因此,reinvokes內

Drupal.behaviours.myModule = { 
    attach : { 
    // your code here 
    // Code here will get executed on page load and every ajax request 

     new Drupal.MyMenuList() // this will attache events and handles it 
    } 
} 

的一切這就是比方說,您使用的是一些構造對象附加事件,處理事件,和其他東西一樣

Drupal.MyMenuList = function() { 
    $('#asdf').click(function() { 
    // 
    }); 
} 
Drupal.MyMenuList.prototype.click = function(){ 
    // event handler 
} 

如果你不想使用以上附加行爲,那麼您可以使用Drupal.attachBehaviours(new Drupal.MyMenuList())再次應用相同的行爲。

相關問題