[英]Uncaught Error: No define call for when loading non AMD script by require.js
I have one non AMD javascript that contain my custom functions such as: 我有一个非AMD javascript,其中包含我的自定义函数,例如:
function getItemIndexById(items, id){
for(var i = 0; i < items.length; i++){
if(items[i].ID == id) return i;
}
return false;
}
//more than one define custom function here.
Here main.js file : 这里是main.js文件:
requirejs.config({
enforceDefine: true,
paths: {
"jquery": "libs/jquery/jquery-min",
"underscore": "libs/underscore/underscore-min",
"backbone": "libs/backbone/backbone-min",
"custom" : "libs/scripts/customejs"
},
shim: {
"underscore": {
deps: [],
exports: "_"
},
"backbone": {
deps: ["jquery", "underscore"],
exports: "Backbone"
}
}
});
Then I define in my view : 然后我在我的视图中定义:
define(["jquery" ,
"underscore" ,
"backbone" ,
"custom"
],function($ , _ , Backbone, Custom){
//.....
}
I got an error in Uncaught Error: No define call for custom
. 我在Uncaught Error: No define call for custom
遇到Uncaught Error: No define call for custom
。
Do I have to convert my custom js into AMD? 我是否必须将自定义js转换为AMD? Could anyone explain me about this issue please. 请问有人能解释一下这个问题。 Thanks. 谢谢。
There are a few common reasons for this issue described in the Require documentation . Require文档中描述了此问题的一些常见原因。
In this case it is most likely because you are using enforceDefine: true
and the "custom" js file does not call define()
. 在这种情况下,最有可能是因为您使用的是enforceDefine: true
,并且“ custom” js文件未调用define()
。
You will need to set enforceDefine: false
or add a proper shim for the custom code. 您需要设置enforceDefine: false
或为自定义代码添加适当的填充程序。
The purpose of a shim is to allow Require to load non-AMD code. 填充程序的目的是允许要求加载非AMD代码。 It works by loading the code and verifying that the script created a property in the global space, as defined by the exports
property. 它的工作方式是加载代码并验证脚本是否在全局空间中创建了exports
属性所定义的属性。
In your case, you could use getItemIndexById
as the exports
value: 在您的情况下,您可以使用getItemIndexById
作为exports
值:
shim: {
"custom": {
exports: "getItemIndexById"
}
When you used Custom
as the exports
value it didn't work because your script did not create a variable called Custom
当您使用Custom
作为exports
值时,它不起作用,因为您的脚本没有创建名为Custom
的变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.