2010-10-04 22 views
3

即時通訊使用[drupal_add_js_()][1]函數添加fancybox效果我的節點(即時通訊不使用Fancybox模塊,因爲不適合我的需要)。Drupal:添加一個js函數回調Drupal.settings

總之,我需要添加titleFormat函數來格式化圖像標題;在我的JavaScript文件,它看起來像:

$("a[rel=myfancy_group]").fancybox({ 
    'transitionIn': 'elastic', 
    'transitionOut': 'elastic', 
    'titlePosition': 'over', 
    'titleFormat': function(title, currentArray, currentIndex, currentOpts) { 
     return '<span><b>' + title + '</b> | Image ' + (currentIndex + 1) + ' of ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>'; 
    } 
}); 

這是我drupal_add_js功能的外觀:

drupal_add_js(
    array(
     'mycustom_fancybox' => array(
      'selector' => 'div.field-field-immagini-minigallery a', 
      'group' => TRUE, 
      'options' => array(
       'transitionIn' => 'elastic', 
       'transitionOut' => 'elastic', 
       'titlePosition' => 'inside', 
       'titleShow' => TRUE, 
       'width' => 500, 
       'cyclic' => TRUE, 
       'titleFormat' => ??? 
      ) 
     ) 
    ), 
    'setting', 
    'footer', 
    FALSE, 
    TRUE, 
    TRUE 
); 

編輯我已經tryed:

//add fancybox settings 
drupal_add_js(
    array(
     'mycustom_fancybox' => array(
      'selector' => 'div.field-field-immagini-minigallery a', 
      'group' => TRUE, 
      'options' => array(
       'transitionIn' => 'elastic', 
       'transitionOut' => 'elastic', 
       'titlePosition' => 'inside', 
       'titleShow' => TRUE, 
       'width' => 500, 
       'cyclic' => TRUE, 
       'titleFormat' => "function my_title_format(title, currentArray, currentIndex, currentOpts) { return '<span><b><i>' + title + '</i></b> | Immagine ' + (currentIndex + 1) + ' di ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>'; }" 
      ) 
     ) 
    ), 
    'setting', 
    'footer', 
    FALSE, 
    TRUE, 
    TRUE 
); 

但(因爲我認爲)Drupal渲染它像:

"function (title, currentArray, currentIndex, currentOpts) { return \'\x3cspan\x3e\x3cb\x3e\x3ci\x3e\' + title + \'\x3c/i\x3e\x3c/b\x3e | Immagine \' + (currentIndex + 1) + \' di \' + currentArray.length + (title.length ? \' \x26nbsp; \' + title : \'\') + \'\x3c/span\x3e\'; }" 

...並沒有工作。

我tryed

drupal_add_js(
    array(
     'mycustom_fancybox' => array(
      'selector' => 'div.field-field-immagini-minigallery a', 
      'group' => TRUE, 
      'options' => array(
       'transitionIn' => 'elastic', 
       'transitionOut' => 'elastic', 
       'titlePosition' => 'inside', 
       'titleShow' => TRUE, 
       'width' => 500, 
       'cyclic' => TRUE, 
       'titleFormat' => 'my_title_format' 
      ) 
     ) 
    ), 
    'setting', 
    'footer', 
    FALSE, 
    TRUE, 
    TRUE 
); 
//and, in my js file, added: 
function my_title_format(title, currentArray, currentIndex, currentOpts) { 
    return '<span><b><i>' + title + '</i></b> | Immagine ' + (currentIndex + 1) + ' di ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>'; 
} 

但同樣doestn工作。

+1

問題的總體目標並不清楚。你想添加'titleFormat'並且你無法使它工作? – 2010-10-04 13:08:33

+0

@NoParrots:我想在Drupal.settings中添加titleFormat選項,但是我不知道如何去做,因爲titleFormat不是一個字符串/布爾值,而是一個函數回調函數。 – Strae 2010-10-04 13:15:41

+0

您是否嘗試過在選項數組中傳遞函數回調?它工作嗎?如果不是,那麼將函數定義放在單引號內怎麼辦? – 2010-10-04 13:33:34

回答

0

看看js Drupal.theme mechanizm。 實施例I從模塊/塊花/ block.js

// A custom message for the blocks page specifically. 
    Drupal.theme.tableDragChangedWarning = function() { 
    return '<div class="warning">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t("The changes to these blocks will not be saved until the <em>Save blocks</em> button is clicked.") + '</div>'; 
    }; 

它可以是有用的。但它真的看起來你只需要分開的js文件,它將得到你需要的Drupal.settings,並將它們插入代碼應用的幻想中。

0

再想一想...爲什麼你需要發送函數回調定義? drupal_add_js的目的是傳遞參數。函數回調從不改變,對吧?您可以隨時在您的自定義JavaScript文件中定義它...

+0

你說得對,會好很多,我試過只傳遞drupal_add_js中的名字函數,但它不起作用(我更新了我的問題) – Strae 2010-10-04 14:09:44