我正在尝试使用requirejs捆绑我的javascripts并在其上构建。有一些现有的js文件是为UI操作和库使用而开发的。
这些js文件需要在jquery包含之后立即包含,并且它还为网站中的社交媒体小部件做了一些事情。任何人,我写下面的代码尝试捆绑它们,以便在加载网站时执行,出于某种原因,我可以看到它们在firebug网络中加载。但是其中的代码失败了,因为它不止一次执行..
以下是我的配置和要求声明,任何人都可以建议这种行为发生的原因?
'use strict';
require.config({
paths: {
jquery: '../vendors/jquery/jquery-1.10.2.min',
underscore: '../vendors/underscore/underscore-min-1.5.1',
backbone_query: '../vendors/backbone-query-parameters/backbone.queryparams',
backbone: '../vendors/backbone/backbone-min-1.0.0',
theme: '../vendors/theme/v1.9/scripts/'
},
shim: {
'jquery': {
exports: '$'
},
'underscore': {
exports: '_'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'theme/base': {
deps: ['jquery']
},
'theme/message': {
deps: ['jquery', 'theme/base']
},
'theme/input_counter': {
deps: ['jquery', 'theme/base', 'theme/message']
},
'theme/placeholder': {
deps: ['jquery', 'theme/base', 'theme/message', 'theme/input_counter']
},
'theme/scrollbar': {
deps: ['jquery', 'theme/base', 'theme/message', 'theme/input_counter', 'theme/placeholder']
},
'theme/fields': {
deps: ['jquery', 'theme/base', 'theme/message', 'theme/input_counter', 'theme/placeholder', 'theme/scrollbar']
},
'theme/validate': {
deps: ['jquery', 'theme/base', 'theme/message', 'theme/input_counter', 'theme/placeholder', 'theme/scrollbar', 'theme/fields']
},
'theme/main': {
deps: ['jquery', 'theme/base', 'theme/message', 'theme/input_counter', 'theme/placeholder', 'theme/scrollbar', 'theme/fields', 'theme/validate']
}
}
});
require(['jquery', 'theme/base', 'theme/message', 'theme/input_counter',
'theme/placeholder', 'theme/scrollbar', 'theme/fields', 'theme/validate', 'theme/main']);
答案 0 :(得分:1)
我在这里注意到两件事:
你的requirejs引导程序文件(你的配置文件)应该是这样的,将main.js作为你的js文件来初始化你的主骨干视图:
requirejs.config({
paths: {
jquery: '../vendors/jquery/jquery-1.10.2.min',
underscore: '../vendors/underscore/underscore-min-1.5.1',
backbone_query: '../vendors/backbone-query-parameters/backbone.queryparams',
backbone: '../vendors/backbone/backbone-min-1.0.0'
},
shim: {
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'underscore': {
deps: ['jquery'],
exports: '_'
}
}
});
require(['theme/main']
function(main){
main.initalize();
});
然后在各个js文件里面的define()函数告诉requirejs相应地加载你的其他文件。