Javascript CWSpear的悬停下拉菜单amd准备好了吗(require.js)?
我试图使用require.js来使用带有悬停的下拉菜单。 但我一直得到一个错误:“错误:脚本错误”。 要在require.js中使用它,我需要做什么 编辑: 为了帮助@Jake聚焦问题,我做了以下配置:Javascript CWSpear的悬停下拉菜单amd准备好了吗(require.js)?,javascript,twitter-bootstrap,drop-down-menu,requirejs,Javascript,Twitter Bootstrap,Drop Down Menu,Requirejs,我试图使用require.js来使用带有悬停的下拉菜单。 但我一直得到一个错误:“错误:脚本错误”。 要在require.js中使用它,我需要做什么 编辑: 为了帮助@Jake聚焦问题,我做了以下配置: requirejs.config({ paths: { "myBootstrap": _myBootstrappingPath + "js/bootstrap", "myControllers" : _myBootstrappingPath + "js/c
requirejs.config({
paths: {
"myBootstrap": _myBootstrappingPath + "js/bootstrap",
"myControllers" : _myBootstrappingPath + "js/controllers",
//jquery
"jquery": _libsPath + "jquery/1.9.1/js/jquery",
"jquery_validate": _libsPath + "jquery/validate/1.12.0/js/jquery.validate",
//Bootstrap related
"twitterBootstrap": _libsPath + "bootstrap/2.3.1/",
"select2" : _libsPath + "select2/3.3.2/select2",
//misc
"underscore": _libsPath + "underscore/1.4.4/js/underscore",
"backbone": _libsPath + "backbone/1.0.0/js/backbone",
"backbonePageable": _libsPath + "backbone/backbone-pageable/1.2.0/js/backbone-pageable",
"backgrid": _libsPath + "backgrid/0.2.0/backgrid",
"backgridAssets": _libsPath + "backgrid/0.2.0/assets/js",
"backgridExtensions": _libsPath + "backgrid/0.2.0/extensions",
//plugins and extensions
"plugins_datetimepicker": _pluginsPath + "/datetimePicking/bootstrap-datetimepicker",
"plugins_dropdownHover": _pluginsPath + "/dropdownHover/dropdownHover",
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: ["underscore", "jquery"],
exports: "Backbone"
},
bootstrap: {
deps: ["jquery"],
exports: "$.fn.popover"
},
'select2': ["jquery"],
'backgrid': {
deps: ["jquery", "backbone"],
exports: "Backgrid"
},
'backbonePageable': {
deps: ["jquery", "underscore", "backbone"],
exports: "PageableCollection",
init: function(nhonho){
Backbone.PageableCollection = PageableCollection;
}
},
plugins_datetimepicker: {
deps: ["jquery", "bootstrap"]
},
plugins_dropdownHover: {
deps: ["jquery", "bootstrap"]
}
}
});
并将其用于:
(function (bs) {
require(["jquery",
"twitterBootstrap",
"select2",
"plugins_datetimepicker",
"plugins_dropdownHover",
"myControllers/defaultController"], function ($) {
var defaultCtrlr = new ticket.defaultController(bs);
bs.onInit();
defaultCtrlr.start(bs.options);
bs.onReady();
});
})(window.my.bootstrap);
只要我在defines中注释
“plugins\u dropdownhave”
行,它就可以正常工作。如果它试图加载该脚本,则会失败。任何东西都可以通过AMD或通过的方式与RequireJS一起使用
首先,您需要知道该模块是否与AMD兼容。检查的最好地方是源代码。在RequireJS中,所有AMD模块都使用define
-函数声明。如果源代码中没有对上述函数的引用,您可以非常确定您的库/插件/任何不支持AMD的东西都是现成的。如果存在define
,您可以通过在路径中声明文件来导入该文件
谢天谢地,RequireJS允许您通过define
来填充那些没有声明其内容可编辑的javascript文件。它的基本功能是,告诉它文件的依赖项是什么,以及它应该在窗口中插入什么,RequireJS将确保在加载dep之前加载,并在加载之后获取结果
paths: {
'otherlib': 'path/to/otherlib',
'mylib': 'path/to/mylib' // define alias for mylib
}
shim: {
'mylib': {
deps: ['otherlib'],
exports: 'MyLib'
}
}
由于您试图加载的文件是一个jquery插件,因此不需要导出或其他任何东西,因为该插件只是将其内容粘贴到$
中。RequireJS提供了一个很好的示例
注意在示例中,RequireJs被配置为不需要使用别名,因为baseUrl
指向带有jquery和插件的目录
希望这有帮助 我发现您的配置可能存在一些问题:
1:在路径配置和垫片配置之间的命名可能会有一些混淆。例如,我在shim中看到:
bootstrap: {
deps: ["jquery"],
exports: "$.fn.popover"
},
但在路径中,你有这样一个:
"twitterBootstrap": _libsPath + "bootstrap/2.3.1/",
他们应该匹配。我猜想,当您需要'plugins\u dropdownhaver'
时,它会查找垫片依赖项'bootstrap'
,然后再也找不到加载它的路径,因为您的路径配置包含'twitterBootstrap'
而不是'bootstrap'
2:\u libsPath+“bootstrap/2.3.1/”
看起来像文件夹的路径,而不是文件。当您稍后需要“twitterBootstrap”并查看Chrome开发工具的网络选项卡(或您使用的任何工具)时,您是否看到加载了正确的引导文件
如果你是只是填充这一个库,我想它会像这样:
requirejs.config({
paths: {
jquery: _libsPath + "jquery/1.9.1/js/jquery",
// note: full path to file (minus the .js)
bootstrap: _libsPath + "bootstrap/2.3.1/bootstrap",
plugins_dropdownHover: _pluginsPath + "/dropdownHover/dropdownHover"
},
shim: {
jquery: {
exports: ["jQuery", "$"]
},
bootstrap: {
deps: ["jquery"],
exports: "$.fn.popover"
},
plugins_dropdownHover: {
// might be able to list just bootstrap here since it will follow the chain
// and load jQuery before loading bootstrap
deps: ["bootstrap", "jquery"]
}
}
});
该列表只列出了三个脚本:jQuery、引导和插件。这正是RequireJS使用上面的垫片配置加载它们的顺序。大多数肯定会有帮助,但我认为我的问题是试图让CWSpear的引导插件与require.js一起工作。我用我的配置更新了我的问题。你能看到它有什么奇怪的地方吗?
requirejs.config({
paths: {
jquery: _libsPath + "jquery/1.9.1/js/jquery",
// note: full path to file (minus the .js)
bootstrap: _libsPath + "bootstrap/2.3.1/bootstrap",
plugins_dropdownHover: _pluginsPath + "/dropdownHover/dropdownHover"
},
shim: {
jquery: {
exports: ["jQuery", "$"]
},
bootstrap: {
deps: ["jquery"],
exports: "$.fn.popover"
},
plugins_dropdownHover: {
// might be able to list just bootstrap here since it will follow the chain
// and load jQuery before loading bootstrap
deps: ["bootstrap", "jquery"]
}
}
});