要求使用shim配置的原因和时间 [英] Requirejs why and when to use shim config

查看:179
本文介绍了要求使用shim配置的原因和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里阅读 requirejs 文件 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.

推荐答案

垫片的主要用途是不支持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.

这篇关于要求使用shim配置的原因和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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