2

I am new to both Backbone and Require JS.

I am receiving the following error:

Cannot read property View of undefined.

So Backbone is not available, but I cannot see where the problem is. Any help would be much appreciated.

I have the following 3 files:

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script data-main="boot" src="../libs/require/require.js"></script>
  </head>
  <body>
    <div id="main">Magic!</div>
  </body>
</html>

boot.js:

require.config({
  baseUrl: "../",
  paths: {
      jquery : "libs/jquery/jquery-1.8.3.min",
      underscore : "libs/underscore/underscore-min",
      backbone : "libs/backbone/backbone-min"
  }
});

require( [ "app/views/app" ], function(AppView) {
  var app = new AppView();
});

app.js:

define([
    "jquery",
    "underscore",
    "backbone"
    ], function( $, _, Backbone ) {

    var AppView = Backbone.View.extend({
        el: $( "#main" ),
        initialize: function () {
             this.render();
        },
        render: function() {
            this.el.html("huzzah!");
        }
    });
    return AppView;

});
1

1 Answer 1

3

You're going to need to shim backbone in order for it to work with requirejs.

From the RequireJS docs:

requirejs.config({
    shim: {
        'backbone': {
            //These script dependencies should be loaded before loading
            //backbone.js
            deps: ['underscore', 'jquery'],
            //Once loaded, use the global 'Backbone' as the
            //module value.
            exports: 'Backbone'
        }
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, in its own file and it gets included directly from the html. I usually call it 'config.js'. TodoMVC has an excellent example of using backbone and require. addyosmani.github.com/todomvc/dependency-examples/…

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.