使用带有Colorbox的requirejs:未捕获的TypeError

时间:2013-11-11 16:33:36

标签: javascript jquery requirejs colorbox

因此,点击按钮后,我想显示一个包含youtube链接的灯箱。

我在项目中使用requirejs和lightbox,但是我收到错误: “未捕获的TypeError:对象函数(e,t){return new x.fn.init(e,t,r)}没有方法'colorbox'”

我认为该功能找不到Colorbox,但我不知道为什么。

这是我的文件:openYoutubeLink:

define( [ 'modules/common/preferences' ], function ( preferences ) {
  return function () {
    $.colorbox({width:"900px", height:"600px", iframe:true, href:"youtube.de"});
  };
});

这是main.js的一部分,带有require.config:

paths: {
    colorbox : 'libs/jquery/jquery.colorbox-min'
}

shim: {
    'colorbox' : { deps: [ 'jquery' ], exports: 'jquery' }
}

1 个答案:

答案 0 :(得分:0)

我会这样做,以便主模块需要jQuery和colorbox插件。我需要它们,以便我确定它们都在我的模块执行时被加载。目前,您看起来已经定义了全局$,但这看似偶然而不是设计。

因此,经过修改后,它将如下所示:

define(['modules/common/preferences', 'jquery', 'colorbox'], 
       function (preferences, $) {
  return function () {
    $.colorbox({width:"900px", height:"600px", iframe:true, href:"youtube.de"});
  };
});

另外,我不认为这是你眼前问题的根源,但我应该提到exports这里错了:

shim: {
    'colorbox' : { deps: [ 'jquery' ], exports: 'jquery' }
}

您希望拥有一个特定于要填充的模块的符号。例如,我使用jQuery的cookie插件并像这样填充它:

'jquery.cookie':{      deps:[“jquery”],      出口:“jQuery.cookie”    }

jQuery.cookie是一个符号,当且仅当 jquery.cookie文件已正确加载并执行时才定义

(kryger建议不要定义exports。虽然RequireJS文档确实说像jQuery或Backbone那样的插件不需要定义exports,但是相同的文档然后提到了如果不使用exports,错误条件的检测将不起作用。我认为最佳做法是始终定义exports而不是等到最终出现问题。)