Expert wanna be
Expert wanna be

Reputation: 10624

RequireJS, how to load js file

I'm trying to load app.js file in main.js, but I'm getting:

TypeError: App is undefined

index.html

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

/js/main.js

require([
    'app'
],function(App){
    console.log(App); // App is undefined
    App.initialize();
});

/js/app.js

define([
    'router'
], function(Router) {
    var initialize = function() {
        Router.initialize();

        return {
            initialize : initialize
        }
    }
});

What am I doing wrong?

Upvotes: 0

Views: 1505

Answers (1)

kryger
kryger

Reputation: 13181

When you require module 'app', the App variable will refer to whatever the 'app' module returns. However, in your case it does not return anything:

function(Router) {
    var initialize = function() {
        Router.initialize();

        return {
            initialize : initialize
        }
    }

    // returns nothing, i.e. undefined
}

Looks like you should move the return block outside the initialize function, essentially it could be rewritten as:

function (Router) {
    return {
        initialize: function() {
            Router.initialize();
        }
    }
}

Upvotes: 2

Related Questions