javascript - RequireJS shim config for Underscore -


requirejs provides shim configuration loading traditional/legacy libraries not support amd. shim config allows exporting said library variable.

'backbone' : {             deps : ['underscore', 'jquery'],             exports : 'backbone' } 

as shown above, backbone not amd compatible hence shim config used export library variable. can done underscore.

'underscore' : {             exports : '_' } 

but if not specify shim config underscore, still works fine. how ? can please shed insight on ?

backbone has dependency of underscore. not necessary dependency has obtained require , other things such shim.
backbone expect _ there in global namespace containing interface underscore library.

when require backbone, app developer provide dependencies using shim option deps property tell dependencies there particular module.

exports property way tell require use property global namespace when referring non amd module, such backbone or _.

so answer question. if not provide shim config underscore following line fail.

var _ = require('underscore'); 

variable _ in local namespace undefined in case, , backbone using _ global namespace.

to answer doubt. how work in backbone source.

// require underscore, if we're on server, , it's not present.     var _ = root._;     if (!_ && (typeof require !== 'undefined')) _ = require('underscore'); 

above extract of backbone source requires underscore. evident comments above require server side. root object when in browser refers window. first check "!_" returns false when in browser cause _ available in gloabl object aka window. require doesn't happens in browser(if underscore present).


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -