Search code examples
backbone.jsrequirejsr.js

After r.js build Backbone and Handlebars are undefined in App Loader


I have a problem with an r.js build in my app loader.

Unbuild mode is working perfect but after a build with r.js the variables in app_loader.js#L7 bb and hb are undefined. So far I work around by using global variables Handlebars und Backbone but what is wrong with this shim?


Solution

  • I have removed your global references and tested this locally. It works.

    build.js - updated to use app_main as config file

    ({
        optimizeCss: "standard",
        removeCombined: true,
        preserveLicenseComments: false,
        appDir: "../www-root-dev",
        dir: "../www-root",
        keepBuildDir: false,
        optimize: "uglify2",
        mainConfigFile: "../www-root-dev/assets/js/app_main.js",
        modules: [{
            name: "app_main"
        }]
    })
    

    app.js

    define(["app_loader"], function(loader){
        var $ = loader.$, Backbone = loader.Backbone;  
        ...
    });
    

    app_loader.js

    define(["jquery","underscore","backbone","handlebars","app_routes"],
    function($, _, Backbone, Handlebars, router){ 
        return {
            $: $.noConflict(),
            _: _,
            Backbone: Backbone,
            router: router,
            Handlebars: Handlebars
        };
    });
    

    app_main.js - updated to simplify paths

    require.config({
        baseUrl: './assets/js',
        paths: {
            mvc: '../../mvc'
        },
        shim: {
            underscore: {
                exports: '_' //the exported symbol
            },
            backbone : {
                deps: ['underscore', 'jquery'],
                exports: 'Backbone' 
            },
            handlebars: {
                deps: ['jquery'],
                exports: 'Handlebars'
            }
        }
    });
    
    require(['app'], function(App){  
        App.initialize(); 
    });
    

    app_routes.js

    define(["jquery", "underscore", "backbone", "mvc/demo.view.js", "mvc/demo.model.js"], function($, _, Backbone, DemoView, DemoModel) { ... });
    

    demo.model.js

    define(["backbone"], function(Backbone) { ... });
    

    demo.view.js

    define(["jquery","backbone","text!mvc/demo.html"], function($, Backbone,demoTemplate) { ... });