Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requirejs using shim

Using shim by requirejs2, there is a way to tell to requires that a module is already loaded?

Example:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="undescrore.js"></script>
<script type="text/javascript" src="require.js'"></script>
<script type="text/javascript">
    require.config({
        paths: {
            "backbone": '/vendor/js/backbone-min.js'
        },
        shim: {
            'backbone': {
                //These script dependencies should be loaded before loading
                //backbone.js
                deps: ['underscore', 'jquery'], // here I would like to load the already loaded library
            }
        }
    });
</script>
like image 743
js999 Avatar asked May 29 '12 16:05

js999


People also ask

What is shim RequireJS?

shim: Configure the dependencies, exports, and custom initialization for older, traditional "browser globals" scripts that do not use define() to declare the dependencies and set a module value. Here is an example. It requires RequireJS 2.1. 0+, and assumes backbone. js, underscore.

What is shim in magento2?

Shim gives a guarantee to load the dependent files first always. If the dependent file will be not found, your js file will be not loaded. In the above syntax, if the jquery file is not loaded, the slick. js file will be never called on the page.

What is RequireJS used for?

RequireJS is a JavaScript library and file loader which manages the dependencies between JavaScript files and in modular programming. It also helps to improve the speed and quality of the code.

What is shim in jQuery?

jQuery uses shim configuration to define the dependencies for jQuery plugins and set a module value by declaring dependencies.


2 Answers

Well, if underscore is already loaded and available, you do not need the shim at all. Backbone will happily load. If not, it's probably because underscore is not actually loaded.

It sounds however wrong to be only partially using require.js, you might as well AMD-load them all. To do that you will need to change your shim like this:

shim: {
    backbone: {
        deps: ["underscore", "jquery"],
        exports: "Backbone"
    },

    underscore: {
        exports: "_"
    }
}

and of course update your paths.

like image 79
ggozad Avatar answered Sep 23 '22 03:09

ggozad


I am not sure if you were able to find the best approach yet for your use case. If you really - for some reason - need to add your other scripts without using RequireJS as in your code example @js999, then you would need to check if the global variable (jQuery, _) of those scripts exist and then define them as modules. From your code example it would look something like this:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="undescrore.js"></script>
<script type="text/javascript" src="require.js'"></script>
<script type="text/javascript">
    // check for jQuery 
    if (window.jQuery) {
        define('jquery', [], function () {
            return window.jQuery;
        });
    }

    // check for underscore
    if (window._) {
        define('underscore', [], function () {
            return window._;
        });
    }

    require.config({
        paths: {
            // remove the file extension (.js)          
            "backbone": '/vendor/js/backbone-min'
        },
        shim: {
            'backbone': {
                deps: ['underscore', 'jquery']
            }
        }
    });
</script>
like image 36
keberox Avatar answered Sep 23 '22 03:09

keberox