【问题标题】:Drupal: add a js function callback to Drupal.settingsDrupal:向 Drupal.settings 添加一个 js 函数回调
【发布时间】:2010-10-04 12:58:33
【问题描述】:

我正在使用[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>';
}

但还是不行。

【问题讨论】:

  • 我不清楚这个问题的总体目标。您想添加 titleFormat 却无法正常工作?
  • @NoParrots:我想在 Drupal.settings 中添加 titleFormat 选项,但我不知道该怎么做,因为 titleFormat 不是字符串/布尔值,需要一个函数回调。
  • 您是否尝试过仅在选项数组中传递函数回调?它有效吗?如果不是,将函数定义用单引号括起来怎么样?
  • 为什么需要在 Drupal.settings 中添加它?那不是放置javascript代码的地方,只有设置。你不能让它工作吗,例如将你拥有的第一段代码添加为“内联”?

标签: php javascript jquery drupal fancybox


【解决方案1】:

看看 js Drupal.theme mechanizm。 我取自 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 并将它们插入到应用的代码中。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 你说得对,会好很多,我尝试在 drupal_add_js 中传递 name 函数,但它不起作用(我已经更新了我的问题)
    猜你喜欢
    • 1970-01-01
    • 2012-04-05
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多