1

I'm not sure about the use of "exports" on shim config, following the example on the requireJS API, I can use Backbone (B in capital letter) to export it to a global scope. This means that it will be a window object property. But I realized that I'm forced to use that name, and I can't export it by other reference name, ie: "MyGlobalBackbone"

require.config({
  paths: {
    backboneAlias:'backbone'
  },
  shim : {
    backboneAlias : {
      deps : [ 'underscore', 'jquery-1.9.1' ],
      exports  : 'MyGlobalBackbone'
    }
  }
});

require(['backboneAlias'],function(backboneAsAliasDependency){
  console.log(backboneAsAliasDependency);//Loaded Ok
  console.log(MyGlobalBackbone); //Uncaught ReferenceError: MyGlobalBackbone is not     defined 
});

This code only works if I use "Backbone" instead of "MyGlobalBackbone"...

opaucara
  • 41
  • 2
  • 7

3 Answers3

7

Actually you got it the other way around: shimming doesn't export a variable to global scope, it imports it FROM the global scope. The name ("Backbone") was set by Backbone's author, and this is the part you're explaining to RequireJS in shim config element.

kryger
  • 12,906
  • 8
  • 44
  • 65
  • then why does shim has an exports tag? – danielrvt Jan 22 '14 at 23:30
  • 1
    @danielrvt you can make it easier by remembering to always read it from the shimmed library's point of view, e.g. *"Backbone: (I'm) depending on jQuery, (I'm) exporting 'Backbone' variable (to global scope)"* – kryger Jan 23 '14 at 00:12
0

See it in the API:
http://requirejs.org/docs/api.html#config-shim

Look at this sentence:

//Once loaded, use the global 'Backbone' as the
//module value.

Let's see it in that way, you will understand it:

//Once loaded, use a global variable 'Backbone' that defined by the backbone vendor as the
//module value.

Liber
  • 422
  • 4
  • 12
-1

You should use map to make an alias.

require.config({
  paths: {
    ...
  },
  shim : {
    ...
  },
  map: {
      '*': {
          'MyGlobalBackbone': 'Backbone'
      }
  }
});

This will allow you to use MyGlobalBackbone instead of Backbone for all (*) modules.

deniss-s
  • 235
  • 2
  • 8