Frage

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?

War es hilfreich?

Lösung

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) { ... });

Andere Tipps

I think the error is because in your shim you have exported backbone and handlebar as Backbone and Handlebars respectively,

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

and in your app_loader.js#L7 you are using it as bb and hb.

Either in shim export it as bb and hb:

backbone : {
             deps: ['underscore', 'jquery'],
             exports: 'bb' 
           },
handlebars:{
             deps: ['jquery'],
             exports: 'hb'
           },

or use Backbone and Handlebars in app_loader.js#L7 instead of bb and hb:

define("app_loader",[ 
  "jquery",
    "underscore",
  "backbone",
  "handlebars",
    "order!assets/js/app_routes.js"
], function(jQuery, underscore, Backbone, Handlebars, router){ 

I am also new to backbone and requirejs but it should help.

I have forked your github project and use grunt requirejs to run the optimization.

I manage to run the site and didn't notice any console errors in Chrome.

I took your project and noticed the errors in your build. I then made some changes to your app_main.js file as follows. Let me know if this solves your issues? Solved mine.

// Require.js allows us to configure shortcut alias
// Their usage will become more apparent futher along in the tutorial.
require.config({

    shim: {
        underscore: {
            exports: '_' //the exported symbol
        },
        backbone : {
            deps: ['underscore', 'jquery'],
            exports: 'Backbone' 
        },
        handlebars: {
            exports: 'Handlebars'
        }

    },

    paths: {
        appLoader:  'app_loader',
        jquery:     'jquery',
        underscore: 'underscore',
        backbone :  'backbone' ,
        handlebars: 'handlebars'

    }
});

require([
'app'
], function(App){  
    App.initialize(); 
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top