Search code examples
javascriptrequirejsamd

RequireJS + oCanvas with self-invoking functions


I have troubles with using oCanvas - it doesn't support AMD out of box so in RequireJS I define a shim: configuration.js

require(
    function () {
        requirejs.config({
            shim: {
                'lib/ocanvas': {
                    exports: ['oCanvas']
                }
            }
        });
    }
);

The way I load shim-related configuration (I think it's enough to add it to require to the entry point file):

require(['configuration','main'],
    function (configuration, main) {
        main.startUniverse();
    }
);

The problem is I cannot get the oCanvas object in my JS files:

define(['lib/ocanvas'],
    function (oCanvas) {}

It appears undefined here. In oCanvas sources I see that there are 2 self-invoking functions and they put the oCanvas object into global state like that: window.oCanvas = oCanvas. Maybe this doesn't work for RequireJS?


Solution

  • Instead of requirejs.config I now use require.config and pass an object there:

    require.config({
        shim: {
            'lib/ocanvas': {
                exports: 'oCanvas'
            }
        }
    });
    

    That worked for me.