Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require.js - undefined modules passed as arguments to define callback

I have the following require js setup for my app:

app.js

require.config({
    paths: {
        jquery: '../thirdparty/jquery-1.9.1.min',
        moment: '../thirdparty/moment',
        spinningwheel: '../thirdparty/datepicker/spinningwheel',
        handlebars: '../thirdparty/handlebars',
        colorgenerator: '../usercolors',
        baseconfig: '../config'        },
    shim: {
        'baseconfig': [],
        'spinningwheel': {
            deps: [],
            exports: 'SpinningWheel'
        },
        'handlebars': {
            deps: [],
            exports: 'Handlebars'
        }

    }
});


require(['jquery', 'dom', 'helpers', 'actions', 'history', 'store'], function ($, dom, helpers, actions, hist, store) {
    //all good on the home front at this point
    //all modules loaded properly
    history.render();
})

history.js

define(['renderview'], function (renderview) {
    //all good here, render view is loaded properly
    return {
        render: function () {
            renderview({...});
        }
    };
})

renderview.js

define(['jquery', 'helpers', 'dom'], function ($, helpers, dom) {
    function renderView(view) {
        var template = helpers.getTemplate(); //ERROR: helpers is undefined!
    }

    return renderView;
});

helpers.js

define(['jquery', 'handlebars', 'history', 'dom', 'colorgenerator'], function ($, Handlebars, history, dom, ColorGenerator) {
    var helpers = {};

    helpers.getTemplate = function () {
       //do stuff
    };

    return helpers;
});

So, as you can see, helpers is loaded just fine in the inital app.js callback, but when I get into renderview.js it is undefined. The dependency arrays in the example are exactly what I have in my actual code, however I've redacted all seemingly irrelevant code. Any ideas why helpers would be loaded fine in app.js but not in renderview.js? Is this a circular dependency? Doesn't look like it to me, but I may have been at this too long today :)

Thanks for any help or suggestions!

Adding current solution, although I don't know why I need to do it this way :)

define(['require', 'jquery', 'dom'], function (require, $, dom) {
    function renderView() {
        //responsible for rendering our view
        var helpers = require('helpers');
        var history = require('history');

        var v = history.get();
        ...
        tmpl = helpers.getTemplate(v.template);
        ...
    }

    return renderView;
});

So, I could not get these modules to load properly without doing the require inside the callback, rather than specifying them as a dependency. Would love some insight to why that is...

like image 636
Greg Avatar asked Nov 12 '22 23:11

Greg


1 Answers

Given the problem and your solution, it looks like you have a circular dependency somewhere in your require calls. Maybe some of the modules that you require in the helper module also require the helpers module.

like image 69
Andreas Köberle Avatar answered Nov 15 '22 00:11

Andreas Köberle