0
votes

The first line of my file looks like this:

define(['plugins/http', 'durandal/app', 'knockout', 'plugins/ajax', 'plugins/formatters'], function (http, app, ko, ajax,formatters) {

Some of my AMD modules load just fine, but some don't, in this example the formatters parameter is undefined.

No errors are shown in the console, and there is a formatters.js file in the same plugins folder with the other plugins that work fine.

How do I debug this? When I put a breakpoint in formatters.js it is being run, so why is the parameter undefined?

I stripped down my formatters js so it has almost nothing in it, just one function, and it still doesn't work:

define(['knockout'], function (ko) {
    'use strict';

    return {
        //convert to number
        rawNumber: function (val) {
            if (val == null)
                return 0;
            else
                return Number(ko.utils.unwrapObservable(val).toString().replace(/[^\d\.\-]/g, ''));
        }
    };
});

Is something wrong with my module, or with my durandal config, or what, has this happened to anyone else that modules are just undefined? What can this mean?

Please help. Thanks!

1

1 Answers

0
votes

Usually when I face this issue with AMD modules it is because I have two modules that reference each other. In this case, the first module is undefined in the context of the second module because it has not finished loading, however the second module loads fine into the first module because it doesn't resolve the alias until it is done loading.

Example -

module = plugins/moduleOne

define(['plugins/moduleTwo'], function (hey) {
    console.log(moduleTwo);
});

module = plugins/moduleTwo

define(['plugins/moduleOne'], function (hey) {
    console.log(moduleOne);
});

In this case moduleTwo resolves properly but moduleOne is undefined. To get around this you can use a require statement in the second module -

function checkModule() {
    if (!moduleOne) {
        moduleOne = require('plugins/moduleOne');
    }
}

Then you could call this method after your second module has activated but before you try to reference moduleOne -

var moduleOne;
checkModule();
moduleOne.doSomething();