Search code examples
javascriptjqueryrequirejsamd

Non AMD libraries using Requirejs


I am working on existing application where we have couple of modules(Non AMD) from some third party. They use name space so for example we have below libraries.

  • DM
  • DM.Voice
  • DM.Audio

Now I am trying to intergrate require js in my project. How do I configure these dependencies. I was looking at shim. But did not really understand it. Can any one give a bit clear explanation about that. Also is it same for using jquery and jquery plugin like scroll?


Solution

  • assuming your public html directory looks like this:

    html
     - index.html
     js
      - jquery.js
      - main.js
      - jquery.scroll.js
      - dm.js
      - require.js
    

    Then in index.html you want:

    <script data-main="js/main" src="js/require.js"></script>
    

    in main.js:

    require.config({
         shim: {
            'dm': {
                exports: 'DM'
           },
    
          "jquery.scroll": ["jquery"]
         }
    });
    
    require( [ 'jquery', 'jquery.scroll'], function( $ ) {
     // use $ here
    });