我很难在drupal模块中正确执行函数。这是一个第三方集成的projekktor HTML5视频播放器。我在drupal.org上找不到任何有用的信息。
(function($) {
Drupal.behaviors.projekktor = {
attach: function(context, settings) {
if ($(settings.projekktor.instances).length) {
$('[id^="projekktor-"]', context).once('projekktor', function() {
$(this).each(function() {
var id = $(this).attr('id');
var jsoptions = settings.projekktor.instances[id];
var jspath = settings.projekktor.jspath;
// @WTF: returns 'projekktor is not defined' if $.get isn't used
$.get(jspath, function() {
projekktor('#' + id, jsoptions);
});
});
})
}
}
};
})(jQuery);程序库正被装进脑袋里。但是,由于某些原因,除非我还将库添加到$.get函数中,否则播放机不会初始化。这会导致事情发展得非常缓慢。
这些设置包含从php传递的数组。‘'jspath’只是库文件的一个字符串。‘'jsoptions’是玩家的选择。那部分运作得很好。我只是不能让玩家正确地开火。
它从内联的$(document).ready函数中初始化an。但这并不是一个可行的选择。
有什么想法吗?
编辑:--在查看了未缩小的projekktor代码之后,我发现了为什么它不能工作。该函数似乎是这样声明的:
jQuery(function($) {
projekktor = $p = function() {
/* projekktor code in here */
}
});它是文档就绪范围中的函数声明。
因此,对于如何使用标头脚本(没有$.get)在函数中激发它,我有点不知所措。
考虑到它是一个第三方库被集成到一个drupal模块中,我有点为难。
发布于 2012-03-28 09:32:47
全局projekktor是在多姆就绪之后声明的。所以你也应该在多姆准备好之后再用它。将您的代码也放入dom准备回调函数中。
尝试:
(function ($) {
$(function () {
Drupal.behaviors.projekktor = {
attach: function (context, settings) {
if ($(settings.projekktor.instances).length) {
$('[id^="projekktor-"]', context).once('projekktor', function () {
$(this).each(function () {
var id = $(this).attr('id');
var jsoptions = settings.projekktor.instances[id];
var jspath = settings.projekktor.jspath;
projekktor('#' + id, jsoptions);
});
})
}
}
};
});
})(jQuery);或者只是:
jQuery(function ($) {
Drupal.behaviors.projekktor = {
attach: function (context, settings) {
if ($(settings.projekktor.instances).length) {
$('[id^="projekktor-"]', context).once('projekktor', function () {
$(this).each(function () {
var id = $(this).attr('id');
var jsoptions = settings.projekktor.instances[id];
var jspath = settings.projekktor.jspath;
projekktor('#' + id, jsoptions);
});
})
}
}
};
});https://stackoverflow.com/questions/9857856
复制相似问题