0

I am trying to load a util.js file that I've created in a separate utils folder into my Backbone application, but no matter what configuration I use for RequireJS, the file is loaded by Require (as I can see it in the dev tools as loaded), but is always undefined in the view itself. As far as I can tell I seem to have defined the file correctly and have tried all different combinations of RequireJS configuration - defining the path in the paths var, defining paths and a shim that exports the name, not defining it in the require config at all but just declaring the path directly in the view define, but it is always undefined.

My folder structure is:


js
 - views
 - controllers
 (etc)
 - utils
   - util.js

The file I'm trying to load is this:

util.js


define(function() {
    return {
        showMessage: function() {
            console.log('show message');
        }
    }; 
});

And like I said I've tried all kinds of Require configs. For example, currently I have:


require.config({
    paths : {
        underscore : 'lib/lodash',
        backbone   : 'lib/backbone-1.0.0',
        marionette : 'lib/backbone.marionette-1.1.0',
        jquery     : 'lib/jquery-2.0.3.min',
        //jquery     : 'lib/jquery-2.0.3',
        tpl        : 'lib/tpl',
        pnotify    : 'lib/jquery.pnotify',
        utils      : 'utils/util'
    },
    shim : {
        'lib/backbone-localStorage' : ['backbone'],
        underscore: {
            exports: '_'
        },
        backbone: {
            exports: 'Backbone',
            deps: ['jquery','underscore']
        },
        marionette: {
            exports: 'Backbone.Marionette',
            deps: ['jquery', 'underscore', 'backbone']
        },
        pnotify: ['jquery'],
        utils: {
            exports: 'utils'  
        } 
    }
});

require(['app'],function(app){
    app.start();
});

And in the calling view I have (modified to just show utils import):

define([
    'utils'
], function (utils) {
    "use strict";

return Marionette.Controller.extend({ 
    initialize: function() {
        console.log(utils); // undefined
    }
});

});

All of my other backbone code (views, models, collections, etc) load fine but I just can't get this file to load. What am I missing here?

0

1 Answer 1

1

you shouldn't need the shim definition. The path declaration should cover your use case.

The shim that you defined tells requirejs that it should look for a global named 'utils' to provide to any module that lists utils/util.js as a dependency. Since you are using requirejs syntax in your utils/util.js and not creating a global, the shim should not be necessary.

Sign up to request clarification or add additional context in comments.

6 Comments

Yeah I've tried not putting anything in the RequireJS config and just passing it into the view/controller callback as 'utils/util' and still undefined, but I can see it loaded in dev tools.
passing it as 'utils' instead of 'utils/util' is a function of the 'paths' configuration, not the 'shim' configuration. Have you tried removing the shim configuration and referencing it as 'utils'?
I created a working gist that demonstrates the problem in your question with the minimum amount of code. gist.github.com/chadxz/10680093 Since gist doesn't allow subdirectories, I put the location of where each file should go in a comment at the top of the file.
Thanks for the suggestion, I had tried that already (defining utils: 'utils/util' in 'path' with no shim, but it didn't work. Tried your change of passing utils into the app.js instead of the controller/view but no luck.
If you post your actual code somewhere i'd be happy to take a look at it when I have some spare time
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.