Drupal:向Drupal.settings添加一个js函数回调

时间:2010-10-04 12:58:33

标签: php javascript jquery drupal fancybox

我正在使用[drupal_add_js_()][1]函数为我的节点中的某些图像添加fancybox效果(我不使用Fancybox模块,因为它不符合我的需要)。

简而言之,我需要添加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
);

编辑我试过:

//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\'; }"

......它不起作用。

我试过

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>';
}

但是再次开始工作。

2 个答案:

答案 0 :(得分:0)

第二个想法......为什么你需要发送函数回调定义? drupal_add_js的目的是传递参数。函数回调永远不会改变,对吧?您始终可以在自定义javascript文件中定义它...

答案 1 :(得分:0)

看看js Drupal.theme mechanizm。 示例I取自module / block / 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并将它们插入代码应用的花哨。