Require js shim doesn't work for this code

I have the following code.

<script src="js/libs/require.js"></script>
<script>
    requirejs.config({
        baseUrl:'js/modules/',
        paths:{
            'bbn':'../libs/backbone',
            'underscore':'../libs/underscore'
        },
        shim:{
            'bbn':{
                exports:'B',
                deps:['underscore']
            }
        }
    })  
    requirejs(['bbn'], function(B){
        console.log(B)
    });
</script>

      

The function parameter B

does not point to Backbone

. Instead, it is registered as undefined

.

I followed the following post and got to this point:

Loading Highcharts with shim using RequireJS and jQuery dependency support

I can see how files underscore

and Backbone

JavaScript are loaded in firebug.

+3


source to share


2 answers


Underlining is also not AMD compatible, so make sure you do too shim

:

requirejs.config({
baseUrl:'js/modules/',
paths:{
    'bbn':'../libs/backbone',
    'underscore':'../libs/underscore'
},
shim:{
    'bbn':{
        exports:'Backbone',
        deps:['underscore']
    },
    'underscore': {
        exports: '_'
    }
}
})  
requirejs(['bbn'], function(Backbone){
console.log(Backbone)
});

      



You will see that Underscore is being loaded, but since it is not defined as the correct module RequireJS just treats it as a regular JS file and does not receive a return value

+4


source


The latest version of Underscore (~ 1.6.0 at the time of writing) is AMD compatible. Do not use it as a spacer or you may run into problems.



+1


source







All Articles