2012-06-24 138 views
4

我试图通过Requirejs模块化我的Backbone应用程序,但我似乎无法得到Requirejs来包含Backbone。这是我main.js是被从索引页包括:无法得到requirejs来包含Backbonejs

require.config({ 
    baseUrl: '/static/js/', 
    paths: { 
     jquery: 'libs/jquery/jquery-min', 
     underscore: 'libs/underscore/underscore-min', 
     backbone: 'libs/backbone/backbone-min', 
     text: 'libs/require/text', 
    }, 
}); 

require(['/static/js/views/app.js'], function(AppView) { 
    var appView = new AppView; 
}); 

这里是我的app.js,但未通过上面的文件包括:

define([ 
    'jquery', 
    'underscore', 
    'backbone', 
], function($, _, Backbone) { 
    console.log($); 
    console.log(_); 
    console.log("inside of the app js file"); 
    var AppView = Backbone.View.extend({ 

     initialize: function() { 
      console.log("inside of appview initialize"); 
     }, 

    }); 
}); 

以下信息会在打印出我的谷歌Chrome控制台:

function (a,b){return new e.fn.init(a,b,h)} 
app.js:7undefined 
app.js:8inside of the app js file 
app.js:9Uncaught TypeError: Cannot read property 'View' of undefined 

$ works的定义,但_和Backbone的定义不起作用。他们没有定义。任何想法为什么发生这种情况?

回答

5

我建议你使用requireJS的版本具有的jQuery捆绑在一起。它会为你节省很多设置痛苦。你可以在这里阅读细节:http://requirejs.org/docs/jquery.html这里下载文件:https://github.com/jrburke/require-jquery

在他们自己的话说:

随着RequireJS,脚本可以以不同的顺序比他们指定的顺序加载。这可能会导致假设jQuery已被加载的jQuery插件出现问题。使用合并的RequireJS + jQUery文件可以确保jQuery在加载任何jQuery插件之前处于页面中。

这应该采取与requireJS正确加载的jQuery的护理:

<script data-main="js/main" src="js/require-jquery.js"></script> 

Main.js

有两点要注意这里:

  • 无需重新定义jquery的路径,因为这已经照顾了
  • 你仍然需要指出的jQuery为必需的模块
  • (我不得不更新的路径,让他们在我的系统工作)

代码:

require.config({ 
    baseUrl: 'js/', 
    paths: { 
     underscore: 'libs/underscore-min', 
     backbone: 'libs/backbone-min', 
    }, 
}); 

require(['jquery', 'app'], function($, AppView) { 
    console.log("done w/ requires"); 
    console.log($) 
    console.log(AppView) 
    var appView = new AppView; 
}); 

app.js

一对夫妇的注意事项:

  • 如果JS文件已被定义为模块,则只能在回调中加载JS文件。所以函数($,_,Backbone)将会失败。
  • 你必须返回你的对象,以便它可以在main.js回调使用(返回APPVIEW)

代码:

define(
    [ 
    'jquery', 
    'underscore', 
    'backbone', 
    ], 
    function() { 
     console.log($); 
     console.log(_); 
     console.log(Backbone); 
     console.log("inside of the app js file"); 
     return AppView = Backbone.View.extend({ 
      initialize: function() { 
      console.log("inside of appview initialize"); 
     }, 
    }); 
}); 

控制台

与该代码在地点,这是控制台输出:

function (selector, context) { 
    // The jQuery object is actually just the init constructor 'enhanced' 
    return new jQuery.fn.init(selector, context, rootjQuery); 
    } [app.js:8] 

function (a){return new m(a)} [app.js:9] 

Object [app.js:10] 

inside of the app js file [app.js:11] 

done w/ requires main.js:21 

function (selector, context) { 
    // The jQuery object is actually just the init constructor 'enhanced' 
    return new jQuery.fn.init(selector, context, rootjQuery); 
} [main.js:22] 

function(){a.apply(this,arguments)} [main.js:23] 

inside of appview initialize [app.js:15] 
5

我会回避使用分叉版本的主干和下划线。 requirejs添加了一个“shim”配置选项来处理本机不支持AMD的第三方库。这使得更新到最新版本的库更容易。

http://requirejs.org/docs/api.html#config-shim

下面是一个例子:

require.config({ 
    paths: { 
    jquery: "libs/jquery", 
    underscore: "libs/underscore", 
    backbone: "libs/backbone" 
    }, 
    shim: { 
    underscore: { 
     exports: '_' 
    }, 
    backbone: { 
     deps: ["underscore", "jquery"], 
     exports: "Backbone" 
    } 
    } 
}); 
相关问题