您的main.js文件将加载提供给它的任何文件路径,只要应用程序的其他元素将它们指定为依赖项。见我的例子main.js文件:
require.config({
paths: {
'app': 'app',
'underscore':'bower_components/underscore/underscore-min',
'backbone':'bower_components/backbone/backbone-min',
'marionette':'bower_components/backbone.marionette/lib/backbone.marionette.min',
'jquery': 'bower_components/jquery/jquery.min',
'tpl':'bower_components/requirejs-tpl/tpl',
'bootstrap':'bower_components/bootstrap/dist/js/bootstrap.min',
'leaflet':'bower_components/leaflet/leaflet',
'leaflet.markercluster':'bower_components/leaflet/leaflet.markercluster',
},
shim: {
'underscore': {
exports: '_'
},
'leaflet': {
exports: 'L'
},
'leaflet.markercluster': {
deps: ['leaflet']
},
'backbone': {
deps: ['underscore']
},
'marionette': {
deps: ['backbone']
},
'jquery': {
exports: '$'
},
'bootstrap': {
deps: ['jquery']
},
'app': {
deps: ['jquery', 'leaflet','bootstrap', 'leaflet.markercluster', 'marionette', 'tpl']
},
'app.elem': {
deps:['app']
},
'app.api': {
deps:['app']
}
}
})
require(['app','app.api','app.elem'], function() {
App.start();
})
,就连最初的应用程序文件:
define(['router', 'collections/moments'], function(router, momentCollection) {
// Boot the app!
App = new Marionette.Application();
App.LocResolve = false; // Have we resolved the user's location?
App.Locating = true; // Are we actively tracking the user's location?
App.FileReader = window.FileReader ? new FileReader : null;
App.Position = null; // Instant access to Lat & Lng of user.
App.MomentsRaw = null; // Keep cached copy of returned data for comparison.
App.Moments = new momentCollection; // Current collection of moments.
App.Markers = new L.MarkerClusterGroup(); // Create Marker Cluster Group
App.View = null; // Current view.
// Marionette Regions
App.addRegions({
header: '#header',
map: '#map',
list: '#list',
modal: '#modal',
});
return App
})
我注意到,你是不是在传递一个配置对象 - 这是故意的吗?如果您使用生成优化器R.js,它会自动为您删除未使用的供应商文件。
简而言之,在require.js配置文件中设置供应商文件的路径,然后在需要特定资产时通过define()调用它们。这将确保只使用您需要的文件。希望这可以帮助!
它加载的所有网页上,因为......它需要所有的网页目前。 'main.js'需要'path/somemodule','somemodule.js'需要'path/someothermodule'。因此,无论何时你包含'main.js',它都会包含'somemodule.js'和'someothermodule.js'。 –
只有当你需要模块时才会触发,所以它不需要在所有页面上,除非你在那里加载它。 – webduvet