0

Here is my require.js load file:

require.config({
    urlArgs: "noCache=" + (new Date).getTime(),
    paths: {
        jquery: "vendor/jquery-1.8.2.min",
        bootstrap: "vendor/bootstrap.min",      
        underscore: "vendor/underscore-min",
        backbone: "vendor/backbone-min",    
        template: "libs/template"    
    }
});

require(["jquery", "bootstrap", "underscore", "backbone", "template", "main"], 
function ($, bootstrap, underscore, backbone, template, main) {

})

And inside the main.js file I have the following code:

define(["jquery", "underscore", "backbone"], function ($, _, Backbone) {
  //Backbone and _ are undefined here, why?
})

So why ,,_" and ,,Backbone" are undefined here, what am I doing wrong?
How to properly use them inside other files?

1 Answer 1

1

If you are new to backbone and require js integration take a look to the following tutorial:

http://backbonetutorials.com/organizing-backbone-using-modules/

You are probably using the non-AMD version of Backbone and underscore. Using shim config you can load any library, even non-AMD modules, here is a shim config snippet from one of my source files:

requirejs.config({
    baseUrl: 'assets/js/',
    paths : {
        'jquery': 'lib/jquery/jquery.min',
        'underscore': 'lib/underscore/underscore-amd-min',
        'backbone': 'lib/backbone/backbone-amd-min',
        'bootstrap': 'lib/bootstrap/bootstrap.min',
    },
    shim: {
        'backbone': {
            deps: ['jquery', 'underscore'],
            exports: 'Backbone'
        },
        'underscore': {
            exports: '_'
        },
        'bootstrap': {
            deps: ['jquery']
        }
    }
});

An alternative solution would be using the AMD version of those libraries you will find them here: https://github.com/amdjs

Download and point to them in your path section.

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

3 Comments

Yeah... i am new to these two, especially require.js (I am just trying to integrate it for the first time). Thanks for the great articles, but can you please be more specific about what needs changing here?
Sure, you have to download the amd supported versions for underscore (github.com/amdjs/backbone) and backbone (github.com/amdjs/backbone) and include those libraries instead of the current ones.
@hjuster I just looked at my source files and realized that I'm also using the none-AMD versions of those libraries. This is possible with shim config. I edited my answer and included an example of shim config.

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.