无法获取requirejs包括Backbonejs [英] Cannot get requirejs to include Backbonejs
问题描述
我想通过Requirejs模块化骨干我的应用程序,但我似乎无法得到Requirejs包括骨干。这里是我的main.js是被从索引页包括:
I am trying to modularize my Backbone app through Requirejs, but I cannot seem to get Requirejs to include Backbone. Here is my main.js that gets included from the index page:
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,但未通过上面的文件包括:
Here is my app.js that gets included from the above file:
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");
},
});
});
以下信息会在我的谷歌浏览器的控制台打印出来:
The following information gets printed out in my Google Chrome console:
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
为$的定义工作,但对于_和骨干的定义不工作。他们拿出为未定义。任何想法,为什么发生这种情况?
The definition for $ works, but the definitions for _ and Backbone do not work. They come up as undefined. Any idea why this is happening?
推荐答案
我建议你使用已绑定的jQuery的requireJS的版本。它会为你节省很多安装的痛苦。你可以在这里阅读细节: http://requirejs.org/docs/jquery.html 并在这里下载文件: https://github.com/jrburke/require-jquery
I recommend you use the version of requireJS that has jQuery bundled. It will save you much setup pain. You can read the details here: http://requirejs.org/docs/jquery.html and download the files here: https://github.com/jrburke/require-jquery
在他们自己的话说:
使用RequireJS,脚本可以以不同的顺序比他们指定的顺序加载。这可能会导致该承担的jQuery已经加载jQuery插件的问题。使用组合RequireJS + jQuery的文件,确保jQuery是在页面之前任何jQuery插件负载。
With RequireJS, scripts can load in a different order than the order they are specified. This can cause problems for jQuery plugins that assume jQuery is already loaded. Using the combined RequireJS + jQUery file makes sure jQuery is in the page before any jQuery plugins load.
这应该采取与requireJS正确加载的jQuery的护理:
This should take care of loading jQuery properly with requireJS:
<script data-main="js/main" src="js/require-jquery.js"></script>
Main.js
有两点要注意这里:
- 无需重新定义jQuery的路径,因为这已经照顾
- 您还必须标明的jQuery作为必需的模块
- (我不得不更新的路径有他们在我的系统工作)
code:
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文件。所以功能($,_,骨干)将无法给你。
- 您必须返回你的对象,以便它可以在main.js回调使用(返回APPVIEW)
code:
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");
},
});
});
控制台
随着地方,code,这是控制台输出:
With that code in place, this is the console output:
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]
这篇关于无法获取requirejs包括Backbonejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!