[英]RequireJS: Cannot read property of undefined in callback
My application loads html using html()
method from jQuery. 我的应用程序使用jQuery中的
html()
方法加载html。 I'm having a problem with running a module tabs
created through RequireJS in the callback function switchScreenCallback()
. 我在运行通过RequireJS在回调函数
switchScreenCallback()
创建的模块tabs
遇到问题。 I tried different variants of making this and I'm not sure if the missing reference is caused by RequireJS or the callback. 我尝试了不同的制作方法,但不确定是否丢失的引用是由RequireJS或回调引起的。 The error always is:
该错误始终是:
Uncaught TypeError: Cannot read property 'switchTab' of undefined
I'm looking for a solution and I can provide a fuller code example if needed. 我正在寻找解决方案,如果需要,我可以提供更完整的代码示例。 How to make sure the tabs object is not
undefined
? 如何确保tabs对象不是
undefined
?
main.js: main.js:
requirejs.config({
paths: {
'jquery': '//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min',
'jqueryui': '//code.jquery.com/ui/1.11.4/jquery-ui',
'viewObject': 'view',
'script': 'script',
'tabs': 'tabs',
},
shim: {
jqueryui: {
deps: ['jquery']
},
jquery: {
exports: '$'
},
viewObject: {
deps: ['jquery', 'jqueryui','tabs'],
exports: 'viewObject'
},
tabs: {
deps: ['jquery', 'jqueryui'],
exports: 'tabs'
}
}
});
requirejs(['script']);
view.js: view.js:
define(['jquery', 'jqueryui', 'tabs'], function($,tabs) {
console.log("tabs test 1",tabs === undefined);// returns true
function callWhenReady(selector, callback, scope, params) {
if ($(selector).closest('body').length) {
callback.call(scope, params);
}
else {
setTimeout(function() {
callWhenReady(selector, callback, scope, params);
}, 1);
}
}
var viewObject = {
switchScreenCallback: function(event) {
console.log("ViewObject: SwitchScreenCallback",event);
console.log("tabs test 2",tabs === undefined);// returns true
console.log("tabs test 3",event.data.tabs === undefined);// returns true
tabs.switchTab(event.data.tab);
//event.data.tabs.switchTab(event.data.tab);
},
switchScreen: function(event) {
$('#maincontainer').load(event.data.screen + '.html');
callWhenReady('#' + event.data.screen, viewObject.switchScreenCallback, viewObject, event);
}
}
return {
"viewObject": viewObject
}
});
tabs.js: tabs.js:
define(['jquery', 'jqueryui'], function($) {
var tabs = {
switchTab: function(tab) {
$('#tabs').tabs('select', tab);
}
}
console.log("tabs created");
return {
"tabs":tabs
}
});
Take a closer look at view.js: 仔细看看view.js:
define(['jquery', 'jqueryui', 'tabs'], function($,tabs) {
...
});
you are loading 3 dependencies, 'jquery', 'jqueryui', 'tabs'
into 2 variables ($, tabs)
. 您正在将3个依赖项
'jquery', 'jqueryui', 'tabs'
加载到2个变量($, tabs)
。 So tabs
is the result of jqueryui
, which is undefined. 因此,
tabs
是jqueryui
的结果,它是未定义的。 the return value of tabs
is not assign to anything. tabs
的返回值未分配给任何东西。
try that instead: 尝试代替:
define(['jquery', 'jqueryui', 'tabs'], function($, dummy, tabs) {
...
});
or just change the dependency order: 或只是更改依赖顺序:
define(['jquery', 'tabs', 'jqueryui'], function($, tabs) {
...
});
FYI: you already declared jqueryui
as a dependency for tabs
(in the shim section), and he returns nothing, so i don't see any reason to call for it again in view.js (and same goes for Jquery if you don't actually use it in this code block) 仅供参考:您已经将
jqueryui
声明为tabs
的依赖项(在shim部分中),并且他不返回任何内容,因此我认为没有任何理由在view.js中再次调用它(如果您不这样做,则对于Jquery也是一样)在此代码块中实际使用它)
I think this is the problem: 我认为这是问题所在:
define(['jquery', 'jqueryui', 'tabs'], function($,tabs) {
The above code will assign $
to jquery
and jqueryui
to tabs
上面的代码将
$
分配给jquery
并将jqueryui
分配给tabs
change it to: 更改为:
define(['jquery', 'jqueryui', 'tabs'], function($, $ui, tabs) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.