admin管理员组文章数量:1312758
I am trying to load a module using require.js, and I have the following in my app.js:
require.config({
baseUrl: "js"
});
alert("hello world"); // for debugging
require(['eh2'], function(eh2) {
alert("nothing here"); // for debugging
});
When I run my application, though, despite the app.js being loaded, the module I'm requiring is never loaded - the "hello world" executes, but the "nothing here" doesn't!
My script tag in the HTML page looks like this:
<script type="text/javascript" src="js/lib/require.js" data-main="app"></script>
And eh2.js is located in the js folder, and it is wrapped in a define statement:
define(["./screens/Screens"], function(screens) {
return {
// code here
};
});
What am I doing wrong? Is require.js silently failing on loading some submodule under screens.js, perhaps?
Here is the code from the Screens module:
define([ "screens/TitleScreen", "screens/GameScreen" ], function(titleScreen, gameScreen) {
return {
screenFuncs: {
"TitleScreen" : titleScreen.TitleScreen,
"GameScreen" : gameScreen.GameScreen,
},
buildScreen: function(data) {
var func = screenFuncs[data.type];
var screen = new func(data.params);
return screen;
},
};
});
Do the paths in the define call need to be relative to the current location of the js file I'm in, or to the root defined in the app.js, anyway?
I am trying to load a module using require.js, and I have the following in my app.js:
require.config({
baseUrl: "js"
});
alert("hello world"); // for debugging
require(['eh2'], function(eh2) {
alert("nothing here"); // for debugging
});
When I run my application, though, despite the app.js being loaded, the module I'm requiring is never loaded - the "hello world" executes, but the "nothing here" doesn't!
My script tag in the HTML page looks like this:
<script type="text/javascript" src="js/lib/require.js" data-main="app"></script>
And eh2.js is located in the js folder, and it is wrapped in a define statement:
define(["./screens/Screens"], function(screens) {
return {
// code here
};
});
What am I doing wrong? Is require.js silently failing on loading some submodule under screens.js, perhaps?
Here is the code from the Screens module:
define([ "screens/TitleScreen", "screens/GameScreen" ], function(titleScreen, gameScreen) {
return {
screenFuncs: {
"TitleScreen" : titleScreen.TitleScreen,
"GameScreen" : gameScreen.GameScreen,
},
buildScreen: function(data) {
var func = screenFuncs[data.type];
var screen = new func(data.params);
return screen;
},
};
});
Do the paths in the define call need to be relative to the current location of the js file I'm in, or to the root defined in the app.js, anyway?
Share Improve this question edited Oct 19, 2012 at 14:51 ekolis asked Oct 19, 2012 at 13:05 ekolisekolis 6,81417 gold badges65 silver badges119 bronze badges 5- Any errors on chrome console or firebug ? – Cristiano Fontes Commented Oct 19, 2012 at 13:09
-
try
define("eh2", ["./screens/Screens"], …);
. Also check whether the screens-script is loaded correctly - have a look at your network panel – Bergi Commented Oct 19, 2012 at 13:11 - require.js won't normally fail silently, you'll get errors (they may be delayed sometimes). Make sure to also turn on asynchronous request logging in whatever browser debug tool you're using. – Matt Whipple Commented Oct 19, 2012 at 13:21
- There is an error in the Screens module on the console, actually - but it's the same sort of thing: it can't find the code in the module I'm loading there. Since there's no formatting allowed in ments, I'll edit the original post to include the code from Screens. – ekolis Commented Oct 19, 2012 at 14:48
- And yes, I tried it without the namespace prefixes on the constructor names, and everything did work before I separated it out into multiple js files and used require! – ekolis Commented Oct 19, 2012 at 14:53
1 Answer
Reset to default 5 +50replace this:
define(["./screens/Screens"], function(screens) {
....
});
either with an absolut path variant:
define(["screens/Screens"], function(screens) {
....
});
or use:
define(function(require) {
var screens = require("./screens/Screens");
....
});
From the docs:
Relative module names inside define():
For require("./relative/name") calls that can happen inside a define() function call, be sure to ask for "require" as a dependency, so that the relative name is resolved correctly:
define(["require", "./relative/name"], function(require) {
var mod = require("./relative/name");
});
Or better yet, use the shortened syntax that is available for use with translating CommonJS modules:
define(function(require) {
var mod = require("./relative/name");
});
本文标签: javascriptrequirejs is not loading any modulesStack Overflow
版权声明:本文标题:javascript - require.js is not loading any modules - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741886259a2403038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论