0

I'm trying to follow the example code that i've seen here and several other places for loading backbone/underscore with require.js. i've basically copy/pasted the example code from Loading Backbone and Underscore using RequireJS but it isn't working.

in app.js the alert(Backbone) displays, Backbone is null. I'm not really sure why it isn't there - can anybody see what i'm doing wrong? thanks for any help.

index.html

<script type="text/javascript" data-main="scripts/main" src="scripts/require.js"></script>

scripts/main.js

require.config({
    paths: {
        'jquery': 'libs/jquery-1.7.1',
        'underscore': 'libs/underscore', 
        'backbone': 'libs/backbone'
    }
});

require([
    'libs/domReady',
    'app/app'
], function(domReady, app){
    domReady(function () {
        app.initialize();
    });
});

scripts/app/app.js

define([
    'jquery', 
    'underscore',
    'backbone'
], function($, _, Backbone){
    return {
        initialize: function(){
            // examples say you can use $, _ or Backbone here
            // but Backbon is null here...  WTF???
            alert(Backbone);
        }
    };
});

Here's the versions I have in case that's important: jquery 1.7.1 require.js version 1.0.4 underscore 1.3.0 backbone version 0.5.3 modelbinding 0.4.3

Community
  • 1
  • 1
Jason
  • 1,726
  • 17
  • 19

1 Answers1

0

Can we see your file structure, with file names? is your backbone file @ libs/backbone.js? or is it libs/backbone-0.5.3.js or something? same goes for underscore and other libraries, the name has to be correct.

Sander
  • 13,301
  • 15
  • 72
  • 97
  • (sorry line breaks don't seem to work in comments) thanks, I basically got rid of all of my code just to get this working so i have no application code or anything. my directory structure looks like this: /index.html /scripts/main.js /scripts/require.js /scripts/app/app.js /scripts/libs/backbone.js /scripts/libs/underscore.js /scripts/libs/jquery-1.7.1.js /scripts/libs/domReady.js – Jason Jan 13 '12 at 23:26
  • i don't mean to suggest this is the correct answer, but i was able to get it to work using the technique on this page: http://backbonetutorials.com/organizing-backbone-using-modules/ by adding a "loader" class and what I am calling a proxy file. when i do this then everything works but it seems like a hack to me, i don't understand why backbone is null unless this loader/proxy technique is used. – Jason Jan 13 '12 at 23:34
  • ok, the proxy file actually helps the loading of non AMD javascript files, so are you sure the backbone and underscore you are trying to include is the AMD version? what version of underscore are you using? and is the backbone version this one?: backbone-0.5.3-optamd3? – Sander Jan 14 '12 at 11:16
  • Aha, I just found my problem. From the underscore.js page: "Upgrade warning: version 1.3.0 removes AMD (RequireJS) support" why would they do that? – Jason Jan 16 '12 at 02:05
  • 1
    I found more reading about the subject here https://github.com/documentcloud/underscore/commit/0d4b1247c45083c695cab4242c084a97aa600221 - it seems the term that is used instead of "proxy" for this situation is "shim" - i was not familiar with that term as used by javascript. it looks like if you want to use the most recent version of underscore then you have to do that. – Jason Jan 16 '12 at 02:29
  • I have updated my answer in [Loading backbone and underscore using requirejs](http://stackoverflow.com/questions/8131265/loading-backbone-and-underscore-using-requirejs/8156341#8156341) – Riebel Jan 16 '12 at 18:14