Drupal: добавить функцию обратного вызова js в Drupal.settings - PullRequest
3 голосов
/ 04 октября 2010

Я использую функцию [drupal_add_js_()][1] для добавления эффекта fancybox к некоторому изображению в моих узлах (я не использую модуль Fancybox, потому что becose не соответствует моим потребностям).

Короче говоря, янеобходимо добавить функцию 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 голосов
/ 05 октября 2010

Посмотрите на 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 и вставит их в сложный код приложения.

0 голосов
/ 04 октября 2010

Если подумать ... зачем вам отправлять функцию обратного вызова определение ? Цель drupal_add_js - передать параметры. Функция обратного вызова никогда не меняется, верно? Вы всегда можете определить его в своем собственном файле JavaScript ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...