最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught Error: No define call for when loading non AMD script by require.js - Stack Overflow

programmeradmin1浏览0评论

I have one non AMD javascript that contain my custom functions such as:

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 :

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.

Do I have to convert my custom js into AMD? Could anyone explain me about this issue please. Thanks.

I have one non AMD javascript that contain my custom functions such as:

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 :

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.

Do I have to convert my custom js into AMD? Could anyone explain me about this issue please. Thanks.

Share Improve this question edited Oct 2, 2013 at 3:18 Nothing asked Oct 2, 2013 at 2:57 NothingNothing 2,64212 gold badges67 silver badges117 bronze badges 6
  • AMD cannot magically know what your script exports. – SLaks Commented Oct 2, 2013 at 3:03
  • I have tried exports : "Custom" in shim, but it didn't work. – Nothing Commented Oct 2, 2013 at 3:04
  • Are those functions being defined in the global scope? If so, are they namespaced? – stavarotti Commented Oct 2, 2013 at 3:12
  • Also, you are not telling us what is causing the error. Is it a call to one of the functions defined in customejs? – stavarotti Commented Oct 2, 2013 at 3:13
  • Defined in global scope but no namespaced, before using require.js, I included it using <script src = "customejs.js" ...>. – Nothing Commented Oct 2, 2013 at 3:15
 |  Show 1 more ment

1 Answer 1

Reset to default 8

There are a few mon reasons for this issue described in the Require documentation.

In this case it is most likely because you are using enforceDefine: true and the "custom" js file does not call define().

You will need to set enforceDefine: false or add a proper shim for the custom code.

The purpose of a shim is to allow Require to load non-AMD code. It works by loading the code and verifying that the script created a property in the global space, as defined by the exports property.

In your case, you could use getItemIndexById as the exports value:

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

Read more about shim here

发布评论

评论列表(0)

  1. 暂无评论