4
votes

I am completely new to Backbone JS. I thought I could solve this minor problem by myself but I can't figure why I am still getting this error:

Uncaught ReferenceError: Backbone is not defined

when trying to extend a Backbone.Model. backbone.js is called before the script that uses it so I don't get it.

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
        <script type="text/javascript" src="js/backbone.js"></script>
        <script type="text/javascript" src="js/main.js"></script>
        <title></title>
    </head>
    <body>

    </body>
</html>

My external file main.js

(function($) {
    window.Doc = Backbone.Model.extend({
        defaults : {
            id : '???',
            title : 'Le titre de mon modèle',
            text : 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer varius ipsum nec porta dignissim. Donec a elementum magna. Donec sagittis magna eu nulla ullamcorper dictum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nullam volutpat felis vehicula, congue mi at, lobortis dolor.',
            keywords : 'lorem ipsum dolor sit amet'
        },
        initialize : function Doc() {
            console.log('Doc Constructor');
        }
    });
})(jQuery);

I also get the following error coming from backbone.js:219

Uncaught TypeError: Cannot call method 'each' of undefined

2
Are you sure your backbone.js file is in the correct place and loaded?Geert Wille
Thanks for reply! Surely, it is correctly loaded with a 304 Not Modified status.D4V1D
Backbone is dependent on Underscore, and you may need json2 as well. See backbonejs.orgWilly
I indeed forgot to load underscore.js. It works now! Thanks!D4V1D

2 Answers

18
votes

You forgot to import underscore.js. It is a Backbone requirement.

Grab it here!

2
votes

I faced the same issue while running jasmine testcases using SpecRunner.html, so when specifying backbone.js in your dependencies in SpecRunner.html, add jquery and underscore.js first, because backbone depends on them.