Ashwin Hegde
Ashwin Hegde

Reputation: 1771

Require.js error says module not loaded

I am using RESTHub 2.0 Backbone Stack

and following is my route.js

define(function (require) {

    'use strict';

    var $ = require('jquery');
    var Backbone = require('backbone');

    var HomeView = require('../views/home');

    var _pageView;

    var AppRouter = Backbone.Router.extend({

        initialize: function() {
            console.log("Router initialize");
            Backbone.history.start({ pushState: false, root: "/" });
            _pageView = new HomeView();
        },

        routes: {
            '': 'main',
            'test': 'test'
        },

        main: function() {
            console.log("Main route activated");
            require(['views/home'], function(HomeView) {
                _pageView.render(HomeView);
            });
        },

        test: function() {
            console.log("Test route activated");
            alert("test");
        }
    });

    return AppRouter;

});

the browser console is giving me following error

Error: Module name "../views/home" has not been loaded yet for context: _
http://requirejs.org/docs/errors.html#notloaded

Why this error is triggered?

I know, this fails because requirejs needs to be sure to load and execute all dependencies before calling the factory function above. If a dependency array is given to define(), then requirejs assumes that all dependencies are listed in that array, and it will not scan the factory function for other dependencies. So, either do not pass in the dependency array.

But still not working!

Upvotes: 1

Views: 3290

Answers (1)

Paul Hoenecke
Paul Hoenecke

Reputation: 5060

In the link given by the error, it says this:

//THIS WILL FAIL
define(['require'], function (require) {
    var namedModule = require('name');
});

I think you need your module in the dependency array:

define(['require', '../views/home'], function (require) {
    var namedModule = require('../views/home');
});

Or, you could just add all your modules to that dependency list, and use the callback function's arguments instead:

define(['jquery', 'underscore', 'backbone', '../views/home'], function ($, _, Backbone, HomeView) {
    // now you dont need to use require(name) at all.
});

Upvotes: 3

Related Questions