Backbone未定义

时间:2016-09-02 13:41:42

标签: backbone.js model

我正在练习backbone.js概念,而我陷入了这个我的控制台显示:

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

我无法在控制台中创建新对象

1 个答案:

答案 0 :(得分:1)

为什么您将script代码添加到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>

工作示例:https://jsfiddle.net/tomi77/bpz0rcg2/