Backbonejs:骨干未定义错误

Kar*_*Rao 4 backbone.js

Google Chrome开发者工具控制台会抛出此错误:

uncaught reference error: backbone is not defined 
Run Code Online (Sandbox Code Playgroud)

当这个html文件包含带有librarybone.js的javascript时:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Example Backbone APP</title>
  </head>
  <body>

    <script>
      Whisky = Backbone.Model.extend();
        firstWhisky = new Whisky({
        name : 'Blenders Pride'
      });
      Whiskies = Backbone.Collection.extend({
        Model:Whisky ,
        url:"#"
      });

      first_view = Backbone.View.extend({
        el : 'body',
        initialize : function() { 
          this.$el.empty();
          this.render();
        } , 

        render : function() { 
          this.$el.append("<h1>The Whisky APP</h1>");
          this.list_view = new List_view();
          this.$el.append(this.list_view.render().el);
          return this ; 
        }
      });
      List_view = Backbone.View.extend({ 
        tagName : 'ul' , 
        render : function() {
          this.$el.empty();
          this.$el.append("<li>Royal stag</li>");
          this.$el.append("<li>Signature </li> ");
          return this ; 
        }
      });
      index_view = new first_view();    
    </script>
    <script src="LIB/json2.js"></script> 
    <script src="LIB/underscore-min.js"></script>  
    <script src="http://code.jquery.com/jquery.js"></script>  
    <script src="http://backbonejs.org/backbone.js"></script> 
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

这个错误的原因是什么?

Mag*_*gus 5

在声明之前你不能使用Backbone.

<script>标记放在您自己的javascript代码之前.

您必须重新整理您的javascript部分,如下所示:

<script src="LIB/json2.js"></script> 
<script src="LIB/underscore-min.js"></script>  
<script src="http://code.jquery.com/jquery.js"></script>  
<script src="http://backbonejs.org/backbone.js"></script> 
<script>
  Whisky = Backbone.Model.extend();
    firstWhisky = new Whisky({
    name : 'Blenders Pride'
  });
  Whiskies = Backbone.Collection.extend({
    Model:Whisky ,
    url:"#"
  });



  first_view = Backbone.View.extend({
    el : 'body',
    initialize : function() { 
      this.$el.empty();
      this.render();
    } , 

    render : function() { 
      this.$el.append("<h1>The Whisky APP</h1>");
      this.list_view = new List_view();
      this.$el.append(this.list_view.render().el);
      return this ; 
    }
  });
  List_view = Backbone.View.extend({ 
    tagName : 'ul' , 
    render : function() {
      this.$el.empty();
      this.$el.append("<li>Royal stag</li>");
      this.$el.append("<li>Signature </li> ");
      return this ; 
    }
  });
  index_view = new first_view();    
</script>
Run Code Online (Sandbox Code Playgroud)