使用require.js加载脚本

时间:2013-04-03 12:01:02

标签: backbone.js requirejs

这是我的require.js加载文件:

require.config({
    urlArgs: "noCache=" + (new Date).getTime(),
    paths: {
        jquery: "vendor/jquery-1.8.2.min",
        bootstrap: "vendor/bootstrap.min",      
        underscore: "vendor/underscore-min",
        backbone: "vendor/backbone-min",    
        template: "libs/template"    
    }
});

require(["jquery", "bootstrap", "underscore", "backbone", "template", "main"], 
function ($, bootstrap, underscore, backbone, template, main) {

})

在main.js文件中,我有以下代码:

define(["jquery", "underscore", "backbone"], function ($, _, Backbone) {
  //Backbone and _ are undefined here, why?
})

那么为什么,, _“和,Backbone”在这里未定义,我做错了什么? 如何在其他文件中正确使用它们?

1 个答案:

答案 0 :(得分:1)

如果您是骨干新手并且需要js集成,请查看以下教程:

http://backbonetutorials.com/organizing-backbone-using-modules/

您可能正在使用非AMD版本的Backbone和下划线。使用shim配置你可以加载任何库,甚至是非AMD模块,这是我的一个源文件的shim配置片段:

requirejs.config({
    baseUrl: 'assets/js/',
    paths : {
        'jquery': 'lib/jquery/jquery.min',
        'underscore': 'lib/underscore/underscore-amd-min',
        'backbone': 'lib/backbone/backbone-amd-min',
        'bootstrap': 'lib/bootstrap/bootstrap.min',
    },
    shim: {
        'backbone': {
            deps: ['jquery', 'underscore'],
            exports: 'Backbone'
        },
        'underscore': {
            exports: '_'
        },
        'bootstrap': {
            deps: ['jquery']
        }
    }
});

另一种解决方案是使用您在此处找到的那些库的AMD版本: https://github.com/amdjs

path部分下载并指向他们。

相关问题