Backbone + r.js:Backbone没有定义

我试图用Grunt和grunt-contrib-requirejs插件优化Backbone项目。 项目具有以下结构:

 - root/ |- public/ | |- components/ | |- js/ | | |- login/ | | | |- [dirs and files] | | | |- main.js | |- libs/ |- gruntfile.js 

componentslibs目录中,我已经使用Bower安装了库。 gruntfile.js有requirejs的以下选项:

  requirejs: { compile: { options: { name: "main", baseUrl: "public/js/login/", mainConfigFile: "public/js/login/main.js", out: "public/js/app.build.js", findNestedDependencies: true, } } }, 

bower.json我有以下依赖关系:

 "dependencies": { "jquery": "2.0.3", "jquery.cookie": "1.4.1", "underscore": "1.6.0", "backbone": "~1.1.2", "backbone-validation": "~0.9.1", "backbone-mediator": "~1.0.0", "backbone.subviews": "~0.7.3", "font-awesome": "~4.1.0", "bootstrap": "~3.2.0", "requirejs": "~2.1.14", "require-handlebars-plugin": "~0.8.1", "pace": "~0.5.5", "modernizr": "~2.8.3", "fastclick": "~1.0.2", "moment": "~2.7.0", "sweetalert": "~0.2.1" } 

最后, main.js有requirejsconfiguration和主应用程序:

 require.config({ waitSeconds: 0, hbs: { templateExtension: 'html', disableI18n: true, disableHelpers: true }, shim: { 'jquery': { exports: '$' }, 'jqueryCookie': { deps: ['jquery'] }, 'underscore': { exports: '_' }, 'backbone': { deps: ['underscore', 'jquery'], exports: 'Backbone' }, 'backboneREST' : { deps: ['backbone'] }, 'backboneValidation' : { deps: ['backbone'] }, 'handlebars': { deps: ['handlebars'], exports: 'Handlebars' } }, paths: { jquery: '../../components/jquery/jquery.min', jqueryCookie: '../../components/jquery.cookie/jquery.cookie', underscore: '../../components/underscore/underscore', backbone: '../../components/backbone/backbone', backboneValidation: '../../components/backbone-validation/src/backbone-validation', backboneSubviews: '../../components/backbone.subviews/backbone.subviews', hbs: '../../components/require-handlebars-plugin/hbs', moment : '../../components/moment/moment', modernizr: '../../components/modernizr/modernizr', sweetAlert: '../../components/sweetalert/lib/sweet-alert', client: '../../libs/client', backboneREST: '../../libs/backbone-rest', viewManager: '../../libs/view-manager', errorManager: '../../libs/error-manager', } }); 

运行咕噜任务给我Done, without errors. 消息,但是当我试图加载在浏览器中产生的脚本,这是给我的Backbone is not defined错误。 源地图说,它正在落在Backbone.Validation 。 任何想法如何解决它?

前几天我也有同样的错误 我修正了使用AMD版本( backbone-validation-amd-min.js )而不是标准版本。

由于Backbone.validation扩展了Backbone,因此您应该在您的Shim上导出Backbone ,这样它就不会被覆盖。

 'backboneValidation' : { deps: ['backbone'], exports: 'Backbone' } 

希望它能解决你的问题。