gpt4 book ai didi

javascript - RequireJS: 模块名称 __ 尚未加载上下文,但仅针对某些,而不是全部

转载 作者:行者123 更新时间:2023-11-29 19:43:47 25 4
gpt4 key购买 nike

现在我知道这个问题已经被问过一百万次了,但我还是想不通。

我在各种文件中有大量的“定义”。大多数时候,这是有效的,但我的一些模块有时“未针对上下文加载”即使我将它们的名称放在“定义”语句中

===========================

当前代码

index.js(针对 StackOverflow 进行了简化)

require.config({
baseUrl: '',
paths: {
//TOOLS
jquery: '../libraries/jquery-1.10.2.min',
socketio:'socket.io/socket.io',
jqueryLightBox:'../libraries/jquery.lightbox_me',
jqueryMigrate:'../libraries/jquery-migrate-1.2.1',
jqueryUI:'../libraries/jquery-ui-1.10.3.custom.min',
spinner:'../libraries/jquery.spin',
jQueryFormPlugin:'../libraries/jquery.form.min',
jQueryCookiePlugin:'../libraries/jquery.cookie',
json2:'../libraries/json2',
raphael:'../libraries/raphael-min',
raphaelPanZoom:'../libraries/raphael.pan-zoom.min',
bootstrap: '../libraries/bootstrap.min',
browserDetection: '../libraries/BrowserDetect',
qtip: '../libraries/jquery.qtip.min',
imagesloaded: '../libraries/imagesloaded.pkgd.min',
eventie: '../libraries/eventie',
eventEmitter: '../libraries/EventEmitter.min',
underscore: '../libraries/underscore-min',
underscoreString: '../libraries/underscore.string.min',
stacktrace: '../libraries/stacktrace',
elapseMeMin: '../libraries/elapseMe.min',
jqueryKeithWood: '../libraries/jquery.svg.min',
domReady: '../libraries/domReady',
//PAGES: (Simplified for StackOverflow)
TestView: '../javascripts/tests/TestView',
TestModel: '../javascripts/tests/TestModel',
TestController: '../javascripts/tests/TestController',
TestPanelView: '../javascripts/tests/Test-StartPanelView',
SocketLogic: '../javascripts/SocketLogic',
Logger: '../javascripts/Logger',

},
shim: {
'socketio': {
exports: 'io'
},
'spinner': {
exports: 'Spinner',
deps: ['jquery']
},
'raphael': {
exports: 'Raphael',
deps: ['jquery']
},
'raphaelPanZoom': {
deps: ['raphael']
},
'bootstrap': {},
'browserDetection': {
exports: 'BrowserDetect'
},
'underscore': {
exports: '_'
},
'underscoreString': {
deps: ['underscore'],
exports: '_'
},
'stacktrace': {
exports: 'printStackTrace'
},
'elapseMeMin': {
exports: 'elapseMe',
deps: ['jquery']
}

}
});

//////////////////
//1) Everything starts with this method. Will execute when the DOM is ready.
/////////////////

require(['domReady'], function (domReady) {
domReady(function () {
//This function is called once the DOM is ready.
//It will be safe to query the DOM and manipulate
//DOM nodes in this function.
//Start Loading!
require(['TestController'], function(TestController)
{
TestController.firstMethod();
});
});
});

TestController.js(针对 StackOverflow 进行了简化)

define(['TestModel', 'TestView', 'SocketLogic', 'Logger', 'TestPanelView'], 
function (TestModel, TestView, socketLogic, Logger, TestPanelView)
{
return {
firstMethod: function()
{
TestModel.doSomething();
TestView.doSomethingElse();
socketLogic.andAnother();
Logger.log("hi");
TestPanelView.updateSomething();
this.anotherMethod();
},
anotherMethod: function()
{
console.log("hello");
}
}
});

===========================

问题

但是,偶尔,我会随机让其中一个“不为上下文加载”,尽管其余的都很好:

Uncaught Error: Module name "TestPanelView" has not been loaded yet for context: _
http://requirejs.org/docs/errors.html#notloaded

===========================

当前修复

修复 #1 - 使用 require('');

代替

TestPanelView.updateSomething();

我一直在做:

var testPanelView = require('TestPanelView');
testPanelView.updateSomething();

哪个有效,但是.. 自从我把它放在标题中后,它不应该已经加载到上下文中吗?为什么是这个?和其他人一样!

修复 #2 - 使用 require([''],function(){});

这也行,但我只是一个接一个地做,感觉不对!

代替

TestPanelView.updateSomething();

有时我会这样做:

require(['TestPanelView'], function(testPanelView){
testPanelView.updateSomething();
});

失败的尝试 #1

TestController.js(针对 StackOverflow 进行了简化)

define(['require', 'TestModel', 'TestView', 'SocketLogic', 'Logger', 'TestPanelView'], 
function (require)
{
var TestModel = require('TestModel')
var TestView = require('TestView');
var SocketLogic = require('SocketLogic');
var Logger = require('Logger');
var TestPanelView = require('TestPanelView');
return {
firstMethod: function()
{
TestModel.doSomething();
TestView.doSomethingElse();
SocketLogic.andAnother();
Logger.log("hi");
TestPanelView.updateSomething();
this.anotherMethod();
},
anotherMethod: function()
{
console.log("hello");
}
}
});

在这里我再次收到“未加载上下文”错误,但这次是针对所有错误。

===========================

想法?

我一直在手动修补我发现的每一个这样的问题,这并非全部发生只有一些地方,感觉就像猴子修补。 .. 我想知道我在这里做错了什么:(有人知道吗?

注意:我使用的是 RequireJS v.2.1.10

最佳答案

对失败尝试 #1 的回答

define(['require', 'TestModel', 'TestView', 'SocketLogic', 'Logger', 'TestPanelView'], 
function (require, testModel, testView, socketLogic, logger, testPanelView) {
var firstMethod = function() {
testModel.doSomething();
testView.doSomethingElse();
socketLogic.andAnother();
logger.log("hi");
testPanelView.updateSomething();
this.anotherMethod();
};
var anotherMethod = function(){
console.log("hello");
}
return {
firstMethod: firstMethod,
anotherMethod: anotherMethod
});

不要忘记,左花括号必须在同一行中函数的结束圆括号之后。

关于javascript - RequireJS: 模块名称 __ 尚未加载上下文,但仅针对某些,而不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21420449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com