RequireJS Uncaught Error:匿名的define()模块不匹配

时间:2013-11-27 10:04:50

标签: javascript backbone.js requirejs

在最小的应用程序中使用RequireJS和Backbone,我总是得到

Uncaught Error: Mismatched anonymous define() module

即使应用程序继续工作。这里是: https://assets.site44.com/admin/

我在index.html中包含jQuery,下划线,骨干,因为我想缩短每个视图/模型中的define()样板。

https://assets.site44.com/admin/js/main.js

组成
 var l = console.log.bind(console)

var app
//l("AA")

require.config({
  paths: {
    // Major libraries
    /*jquery: 'libs/jquery/jquery-min',
    underscore: 'libs/underscore/underscore-min', // https://github.com/amdjs
    backbone: 'libs/backbone/backbone-min', // https://github.com/amdjs
*/
    // Require.js plugins
    text: 'text'

  }

})


function initApp() {
  console.log("BB")

  require([
    'views/AppView'
  ], function(AppView){
    l("CC")

    app = new AppView()
    $("#app").html(app.render())

  })
}

$(document).ready(initApp)

我无法从文档或这个回答的问题中找出问题: Mismatched anonymous define() module

谢谢

2 个答案:

答案 0 :(得分:4)

  

我在index.html中包含jQuery,下划线,骨干,因为我想缩短每个视图/模型中的define()样板。

你不应该。如果您google "Uncaught Error: Mismatched anonymous define() module",您会注意到最上面的链接是针对RequireJS的FAQ解释

  

如果您在HTML中手动编写脚本标记以使用匿名的define()调用加载脚本,则可能会发生此错误。

- 编辑

如果你正在使用grunt,你可以使用grunt-generate根据你自己的自定义模板轻松创建模块,当样板超载可能会毁了你的一天:)

免责声明:我写过Grunt插件。

答案 1 :(得分:0)

找到了如何摆脱它:直接使用require(),而不是在ready处理程序

var l = console.log.bind(console)

var app
//l("AA")

require.config({
  paths: {
    // Major libraries
    /*jquery: 'libs/jquery/jquery-min',
    underscore: 'libs/underscore/underscore-min', // https://github.com/amdjs
    backbone: 'libs/backbone/backbone-min', // https://github.com/amdjs
*/
    // Require.js plugins
    text: 'text'

  }

})


  require([
    'views/AppView'
  ], function(AppView){
    l("CC")

    app = new AppView()
    $("#app").html(app.render())

  })
相关问题