requirejs config里shim配置说明 多文件依赖关系声明

分类:Javascript| 发布:佚名| 查看: | 发表时间:2014/7/2

定义功能模板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({});
});
365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/javascript/1301.html