Search code examples
javascriptrequirejsamd

requirejs defining modules not working as expected


i have the following scenario:

i have a global namespace called fort which has a few common functions that i need and it looks like this :

fort.js

define("fort", ["fortHistory"], function (FortHistory) {

  function Fort(){}
    Fort.prototype.history = FortHistory;

    return Fort;
});

fortHistory is a small module i created defined as so:

fortHistory.js

"use strict";
define("fortHistory", function () {

function FortHistory() {

}
FortHistory.prototype.doSomething = function(){...}
return FortHistory;
});

i then do this in my config.js

require.config( {
    enforceDefine: true,
    paths: {
        'fort': 'develop/js/fort',
        'fortHistory' : 'develop/js/webapp/fortHistory'
 },
    shim: {
        fort:{
            exports: 'fort'
        }
    }
} );
define( function() {} );

finally in main.js i have:

define('fort', [], function(fort){
    window.fort = fort;
});

the hope was that i could then make a call such as :

fort.fortHistory.doSomething();

instead fort is undefined so i am assuming i have misinterpreted how requirejs works


Solution

  • You've named it history, not fortHistory:

    Fort.prototype.history = FortHistory;
    

    Try calling it via fort.history.doSomething();.