user1031947
user1031947

Reputation: 6664

Getting started with RequireJS and Backbone

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;

});

Upvotes: 2

Views: 1336

Answers (1)

AaronAsAChimp
AaronAsAChimp

Reputation: 529

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'
        }
    }
});

Upvotes: 3

Related Questions