2015-05-12 73 views
-1

我是一个受虐狂,所以我决定改变我在我的项目中加载require.js的方式。我所做的只是从这里(用Modernizr.load)去...未捕获的错误:不匹配的匿名定义()模块

<script type="text/javascript"> 

    function requireJsConfig() { 
     requirejs.config({ 
      'baseUrl': '{{ STATIC_URL }}js', 
      'paths': { 
       'jquery': 'libraries/jquery', 
       'backbone': 'libraries/backbone', 
       'underscore': 'libraries/underscore', 
       'jquery.cookie': 'libraries/jquery-cookie', 
       'tinymce': 'tinymce/tinymce.min', 
       'react' : 'libraries/react', 
       'history': 'history' 
      }, 
      waitSeconds: 30, 
      shim: { 
       'jquery' : 'jquery', 
       'underscore' : { 
        exports: '_' 
       }, 
       'backbone': { 
        deps: ['underscore', 'jquery'], 
        exports: 'Backbone' 
       }, 
       'jquery.cookie': { 
        deps: ['jquery'], 
        exports: '$.cookie' 
       }, 
       'tinymce': { 
        deps: ['jquery'], 
        exports: 'tinymce' 
       } 
      } 
     }); 

     define('static_url', [], "{{ STATIC_URL }}"); 
     window.static_url = "{{ STATIC_URL }}"; 
     define('PUBNUB_SUBSCRIBE_KEY', [], "{{ PUBNUB_SUBSCRIBE_KEY }}"); 

     require(["teacher"]); 
    }; 

    Modernizr.load({ 
     load: '{{ STATIC_URL }}js/require.js', 
     complete: function() { 
      requireJsConfig(); 
     } 
    }); 

</script> 

...这(不使用Modernizr.load)...

<script type="text/javascript" src="{{ STATIC_URL }}js/require.js"></script> 

<script type="text/javascript"> 

    function requireJsConfig() { 
     requirejs.config({ 
      'baseUrl': '{{ STATIC_URL }}js', 
      'paths': { 
       'jquery': 'libraries/jquery', 
       'backbone': 'libraries/backbone', 
       'underscore': 'libraries/underscore', 
       'jquery.cookie': 'libraries/jquery-cookie', 
       'tinymce': 'tinymce/tinymce.min', 
       'react' : 'libraries/react', 
       'history': 'history' 
      }, 
      waitSeconds: 30, 
      shim: { 
       'jquery' : 'jquery', 
       'underscore' : { 
        exports: '_' 
       }, 
       'backbone': { 
        deps: ['underscore', 'jquery'], 
        exports: 'Backbone' 
       }, 
       'jquery.cookie': { 
        deps: ['jquery'], 
        exports: '$.cookie' 
       }, 
       'tinymce': { 
        deps: ['jquery'], 
        exports: 'tinymce' 
       } 
      } 
     }); 

     define('static_url', [], "{{ STATIC_URL }}"); 
     window.static_url = "{{ STATIC_URL }}"; 
     define('PUBNUB_SUBSCRIBE_KEY', [], "{{ PUBNUB_SUBSCRIBE_KEY }}"); 

     require(["teacher"]); 
    }; 

    requireJsConfig(); 

</script> 

...现在我得到这个:

Uncaught Error: Mismatched anonymous define() module: function (){return r}

FWIW,这是在错误发生的事情(在require.js):

function intakeDefines() { 
    var args; 

    //Any defined modules in the global queue, intake them now. 
    takeGlobalQueue(); 

    //Make sure any remaining defQueue items get properly processed. 
    while (defQueue.length) { 
     args = defQueue.shift(); 
     if (args[0] === null) { 
      return onError(makeError('mismatch', 'Mismatched anonymous define() module: ' + args[args.length - 1])); 
     } else { 
      //args are id, deps, factory. Should be normalized by the 
      //define() function. 
      callGetModule(args); 
     } 
    } 
} 

顺便说一句,我已经验证these没有适用。

为什么这个看似无害的变化会导致核弹在我的项目中爆炸? (是的,一枚实际的核弹引爆......仿佛数百万的声音突然在恐怖中大声喊叫,并突然沉默......)

回答

0

你说“这些都不适用”,但是除非你手工修改下划线,它certainly does

+0

但我不是在HTML'

0

让我们来看看,如果我能解释它比帕特里克没有更好的...

,因为它检测它是否与AMD的装载机运行时,您不需要为underscore一个shim,如果是这样,那么它调用define对于调用define的模块,请勿使用shim

您还应该删除用于jQuery的shim。除非您使用的是古老版本,否则它也会检测到它是使用AMD加载程序运行并呼叫define。此外shimjquery: "jquery"是没有意义的。 (最接近有意义shim我能想到的是jquery: ["jquery"]这将意味着“jquery取决于jquery”。)

至于为什么当您使用shim与调用define你进入未定义的模块它会前工作...领土:有时它起作用,有时它不起作用。

我不知道其他模块是否需要垫片,因为我不经常使用它们。如果我是你,我会检查他们是否拨打define

+0

谢谢。这在逻辑上是合理的,但是在从垫片中删除jquery,underscore和backbone(所有这些都支持AMD)之后,当我启动我的服务器时会遇到不同的错误,例如'Uncaught TypeError:object is not a function'试图创建主干模型或'$ .ajaxSetup不是函数'。每次启动服务器时,错误都来自需求链中的不同脚本。因此,即使我的第一个必需脚本的第一行是'require(['jquery','underscore','backbone',...])',jquery,underscore和backbone现在还没有被加载。 –

相关问题