我正试图在我的项目中使用普通的 vanilla JS 。我一直都是jQuery粉丝,但我注意到它比原生JS慢了多少;而这个项目速度敏感,所以我需要从设计中挤出一切。
我已经 RequireJS 设置了。我有一个非常简单的设置:
// Configure loading modules from the lib directory,
// except 'app' ones,
requirejs.config({
"baseUrl": "js/lib",
"paths": {
"app": "../app",
"spots": "spots",
"booking": "booking",
"markup": "markup.min",
"cat-filters": "cat-filters",
"common-functions": "common-functions",
"categories": "categories",
"domReady": "domready",
"css-map": "../../maps/jquery.cssmap2"
},
"shim": {
"common-functions": {
"deps": ["domReady"]
},
"categories": {
"deps": ["common-functions"]
}
}
});
if (window.VARS.thisPage == "home") {
require(['domReady','common-functions','css-map'], function(domReady){
}, function (err) {
console.log("Error: " + err)
});
}
然后在(例如),spots.js,我有:
(function(factory){
if (typeof define === "function" && define.amd) {
define(["jquery"], factory);
} else if (typeof exports === 'object') {
factory(require('jquery'));
} else {
factory(jQuery);
}
}(function($, undefined){
// module code here
}));
这适用于jQuery。但是,如果没有它,我永远不会触发代码。我试过从模块中删除包装器代码 - 当它似乎运行时,我在控制台中出错:
Error: Error: Load timeout for modules: common-functions,cat-filters,categories
http://requirejs.org/docs/errors.html#timeout
我做错了什么?谢谢!