Requirejs 为什么以及何时使用 shim 配置 [英] Requirejs why and when to use shim config

查看:27
本文介绍了Requirejs 为什么以及何时使用 shim 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里阅读了 requirejs 文档 API

I read the requirejs document from here API

requirejs.config({
    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) {
                //Using a function allows you to call noConflict for
                //libraries that support it, and do other cleanup.
                //However, plugins for those libraries may still want
                //a global. "this" for the function will be the global
                //object. The dependencies will be passed in as
                //function arguments. If this function returns a value,
                //then that value is used as the module export value
                //instead of the object found via the 'exports' string.
                return this.Foo.noConflict();
            }
        }
    }
});

但我没有得到垫片的一部分.为什么我应该使用 shim 以及我应该如何配置,我能得到更多说明吗

but i am not getting shim part of it. why should i use shim and how should i configure, can i get some more clarification

请任何人举例解释为什么以及何时应该使用垫片.谢谢.

please can any one explain with example why and when should we use shim. thanks.

推荐答案

shim 的主要用途是用于不支持 AMD 但您需要管理它们的依赖项的库.例如,在上面的 Backbone 和 Underscore 示例中:您知道 Backbone 需要 Underscore,因此假设您这样编写代码:

A primary use of shim is with libraries that don't support AMD, but you need to manage their dependencies. For example, in the Backbone and Underscore example above: you know that Backbone requires Underscore, so suppose you wrote your code like this:

require(['underscore', 'backbone']
, function( Underscore, Backbone ) {

    // do something with Backbone

}

RequireJS 将启动对 Underscore 和 Backbone 的异步请求,但是您不知道哪个会先返回,因此 Backbone 可能会在 Underscore 加载之前尝试对其执行某些操作.

RequireJS will kick off asynchronous requests for both Underscore and Backbone, but you don't know which one will come back first so it's possible that Backbone would try to do something with Underscore before it's loaded.

注意:这个下划线/主干示例是在这两个库都支持 AMD 之前编写的.但该原则适用于当今任何不支持 AMD 的库.

NOTE: this underscore/backbone example was written before both those libraries supported AMD. But the principle holds true for any libraries today that don't support AMD.

init"钩子可以让你做其他高级的事情,例如如果一个库通常会将两个不同的东西导出到全局命名空间中,但您想在单个命名空间下重新定义它们.或者,也许您想对正在加载的库中的方法进行一些猴子修补.

The "init" hook lets you do other advanced things, e.g. if a library would normally export two different things into the global namespace but you want to redefine them under a single namespace. Or, maybe you want to do some monkey patching on a methods in the library that you're loading.

更多背景:

  • Upgrading to RequireJS 2.0 gives some history on how the order plugin tried to solve this in the past.
  • See the "Loading Non-Modules" section of This article by Aaron Hardy for another good description.

这篇关于Requirejs 为什么以及何时使用 shim 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆