0

Once in three I got "backbone is not defined error". It's pointing to line 19 in requirejs file (router is declared in path line 21). It's like sometimes backbone is loading fast enough and sometimes not. I don't know what I did wrong. Below a part of the requirejs file and initialization.

require.config({
    paths: {
        //libs
        jquery: '../../bower_components/jquery/dist/jquery.min',
        // "jquery-ui": '../bower_components/jquery-ui/jquery-ui.min',
        lodash: '../../bower_components/lodash/lodash',
        backbone: '../../bower_components/backbone/backbone-min',
        underscore: '../../bower_components/underscore/underscore-min',
        gridstack: '../../libs/gridstack/dist/gridstack',
        text: '../../bower_components/text/text',                                   
        router: '/client/js/router/router',
        //other modules ...
    }

    shim: {
        "backbone": {
            deps: ['underscore','jquery'],
            exports: "backbone"
        },

        "underscore": {
            exports: "_"
        },
        "jquery": {
            exports: "$"
        },
        "gridstack": {
            deps: ['jquery','underscore'],
            exports: "Gridstack"
        }
    });

require([
  'jquery', 'backbone', 'lodash', 'router', 
], function( $, Backbone, _, Router){
    new Router();
    Backbone.history.start();
});

And here is how my router is declared:

define('router',['appView'] , function(AppView) {
    var router = Backbone.router.extend({
    // etc/
François Richard
  • 6,817
  • 10
  • 43
  • 78
  • Possible duplicate of [Loading Backbone and Underscore using RequireJS](http://stackoverflow.com/questions/8131265/loading-backbone-and-underscore-using-requirejs) – Louis Jan 04 '16 at 11:48

1 Answers1

0

One error I see is in your shim for Backbone in the RequireJS configuration. Older versions of Backbone that don't support RequireJS explicitly will export the global variable Backbone, not backbone. So ...

"backbone": {
    deps: [ 'underscore', 'jquery' ],
    exports: 'Backbone'
}

Note that any version of BackboneJS after 1.1.1 supports AMD-style modules, so you should not need a shim at all if you are using a recent version. In fact, using a version that supports AMD, but incorrectly declaring the shim, might be what is causing the 1-in-3 failure... but I'd have to see a running example to know for sure.

Peter Wagener
  • 2,073
  • 13
  • 20