0

I'm trying to use RequireJS in my app. I'm including the requirejs script from cdnjs like this:

<script src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js"></script>

on my page I have a button and I register an event for it:

$('#btnSpeedTest').on('click', function (e) {
    require([baseUrl + 'Content/js/tools/speedtest.js'], function (speedTestModule) {
        alert(speedTestModule);
    });
});

If I watch with Fidler - I see that upon clicking the button speedtest.js is loaded.

speedtest.js contains the following:

define('speedTestModule', function () {
    function SpeedTest(settings, startNow) {
         // basic initialization
    }

    var fn = SpeedTest.prototype;

    fn.startRequest = function (download, twoRequests) {
         // logic
    }

    return SpeedTest;
});

The alert(speedTestModule); command returns "undefined". I saw a tutorial on RequireJS and in that tutorial everything was in the same directory as well as files with names of modules (which is not my case since I'm loading it from CDN).

I even tried to return a simple string, but it did not work. What am I missing?

Thanks

1 Answer 1

1

Don't use a named define. Instead of this:

define('speedTestModule', function () {

do this:

define(function () {

and let RequireJS name your module. You typically want to let r.js add names to your modules when you optimize them. There are a few cases where using names yourself in a define call is warranted but these are really special cases.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.