定义功能模板define
define('camnpr', ['/js/camnpr/camnpr.min.js'], function (require, exports, module) {
return camnpr;
})
实例:以require加载UEditor,配置他们的依赖关系。可以把下边的代码,直接写到require.js
文件里,这个方便管理和加载配置,同时减少一个单独config文件的加载,同时也避免了因为require.js和 config.js加载顺序不同造成的错误。
window.UEDITOR_HOME_URL = "/js/ueditor/";
require.config({
paths: {
'jquery': '/js/jquery',
'uploadify': '/js/uploadify/jquery.uploadify.min',
'editor': '/js/ueditor/ueditor.all.min',
'editorConfig': '/js/ueditor/ueditor.config',
'camnpr': '/js/camnpr/camnpr.min'
},
shim: {
'uploadify': ['jquery'],
'camnpr': ['jquery'],
'editor': ['editorConfig']
}
});
以下是官方例子:
requirejs.config({
//Remember: only use shim config for non-AMD scripts,
//scripts that do not already call define(). The shim
//config will not work correctly if used on AMD scripts,
//in particular, the exports and init config will not
//be triggered, and the deps config will be confusing
//for those cases.
shim: {
'backbone': {
//These script dependencies should be loaded before loading
//backbone.js
deps: ['underscore', 'jquery'],
//Once loaded, use the global 'Backbone' as the
//module value.
exports: 'Backbone'
},
'underscore': {
exports: '_'
},
'foo': {
deps: ['bar'],
exports: 'Foo',
init: function (bar) {
return this.Foo.noConflict();
}
}
}
});
//Then, later in a separate file, call it 'MyModel.js', a module is
//defined, specifying 'backbone' as a dependency. RequireJS will use
//the shim config to properly load 'backbone' and give a local
//reference to this module. The global Backbone will still exist on
//the page too.
define(['backbone'], function (Backbone) {
return Backbone.Model.extend({});
});