0

I have a simple index page that uses a handful of js files, including jquery and underscore from a CDN. To learn, I want to set up requirejs to manage the scripts.

Folder structure:

exampleApp
   -js
     -foo.js
     -main.js
     -require.js
   -index.html

index, note foo() is a function in foo.js:

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


<body>
 <button onclick='foo()'>Click me</button>
</body>

main.js:

requirejs.config({

baseUrl: 'js',
paths: {
    jquery      : '//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js',
    underscore  : '//cdn.jsdelivr.net/underscorejs/1.5.2/underscore-min.js',
    foo         : 'foo'
},
shim: {
    jquery: {
        exports: '$'
    },
    underscore: {
        exports: '_'
    }
}

});

requirejs(['jquery', 'underscore', 'foo'], function($, _, foo){

});

In my foo.js, I use jquery using "$" and underscore using "_", so I want to maintain those symbols if possible.

bmw0128
  • 13,470
  • 24
  • 68
  • 116
  • The answer to the question in the title is "yes". Is there something specific that is causing you a problem? Note that you must pass an array of strings as the first argument to ``require``. – Louis Dec 05 '13 at 18:00
  • thx, I added the strings (typo), and I added shim. I'm not getting an error that i see, it's just that the button in index will not work, the onclick()...i just have foo() outputting a string to the console. It works w/o requirejs, so I'm trying to get it to work with requirejs – bmw0128 Dec 05 '13 at 18:06
  • Your code looks OK. Show the foo.js code. The problem is more likely to be there. – Shuhel Ahmed Dec 05 '13 at 22:45
  • 1
    When loading through RequireJS function `foo()` from module `foo` is not exposed in the global object (i.e. `window.foo()`), you're probably getting a JS error in your console (similar question: http://stackoverflow.com/q/20336455/1240557 ). It's also possible there's something wrong with `foo.js`; you're saying "It works w/o requirejs": is it even an AMD module? Does it start with a `define()`? – kryger Dec 05 '13 at 23:44

0 Answers0