Search code examples
backbone.jsmodel

Backbone Undefined


well i am practicing backbone.js concepts while i am stuck in this my console shows:

main.js:1 Uncaught ReferenceError: Backbone is not defined

index.html

<html>
<head>
<meta charset="UTF-8">
<title>Backbone.js Tutorial</title>
<script src ="main.js"></script>
</head>
<body>
   <script src="js/libs/jquery-1.10.2.min.js"></script>
   <script src="js/libs/underscore-min.js"></script>
   <script src="js/libs/backbone-min.js"></script>
</body>
</html>

main.js

var Animal = Backbone.Model.extend({
    defaults: {
      name: 'Fido',
      color: 'black',
      sound: 'woof'
    },
    validate: function(attrs, options){
      if (!attrs.name){
        alert('Your animal must have a name!');
      }
      if (attrs.name.length < 2){
        alert('Your animal\'s name must have more than one letter!');
      }
    },
    sleep: function(){
      alert(this.get('name') + ' is sleeping.');
    }
});

I am unable to create a new object in my console


Solution

  • Why You put script tags to body?

    <html>
    <head>
    <meta charset="UTF-8">
    <title>Backbone.js Tutorial</title>
       <script src="js/libs/jquery-1.10.2.min.js"></script>
       <script src="js/libs/underscore-min.js"></script>
       <script src="js/libs/backbone-min.js"></script>
       <script src="main.js"></script>
    </head>
    <body>
    </body>
    </html>
    

    working example: https://jsfiddle.net/tomi77/bpz0rcg2/