javascriptrequirejsspin.js

Loading spin.js with require.js


I recently started a Javascript project and I am now moving it to require.js. Everything worked fine so far except for the spin.js library. I get the following error message when I try to access anything spin.js related:

Uncaught ReferenceError: Spinner is not defined

My requirejs.config looks like this:

requirejs.config({

    baseUrl: 'js',

        paths: {
            'jquery': 'lib/jquery',
            'spin': 'lib/spin',
    },

    shim: {
        'jquery' : {
            deps: [],
        },

        'spin' : {
            deps: [],
            exports: 'Spinner'
        },
    }
});  

A sample module looks like this:

require(['spin'], 
    function(Spinner)
    {   
        new Spinner();
    }
);

I'm using the shim config because I have some other modules with dependencies. Everything else seems to be loading fine though. What am I missing here?

Edit:

As Alex pointed out my library inclusion was wrong. For everyone having troubles with understanding backbone.js and require.js I suggest this book, especially the chapter about modular development.


Solution

  • The spin library should not be shimmed in your config. From the spin.js source code:

      if (typeof define == 'function' && define.amd)
        define(function() { return Spinner })
      else
        window.Spinner = Spinner
    

    It is already defined as a module here at the end, and window.Spinner is not created as a window object (which is why it shouldn't be shimmed)