Backbone:Uncaught TypeError:无法调用方法'每个'未定义的

时间:2014-02-26 18:42:44

标签: javascript backbone.js

我正在运行此示例骨干脚本。但我得到一个错误,如'无法调用方法'得到'未定义'。到目前为止,我用我所知道的一切来尝试。但我仍然有问题。任何人都可以帮我解决这个问题。

// JavaScript Document
(function(){

 window.App = {

     Models : {},

     Collections : {},

     Views : {}

     }

  window.template = function(id)
  {
      return _.template($('#'+id).html())
  }  

  //model
  App.Models.Task = Backbone.Model.extend({});

  //view
  App.Views.Task  = Backbone.View.extend({

   tagName : 'li',

   render : function(){

    this.$el.html(this.model.get('title'));

    return this;

   }

  });

  //collection
  App.Collections.Tasks = Backbone.Collection.extend({

   model : App.Models.Task

  });  

  //collection view
  App.Views.Tasks = Backbone.Collection.extend({

   tagName : 'ul',

   initialize : function(){this.render();},

   render : function(){

    this.collection.each(function(t){

     var v = new App.Views.Task({model : t});       

     this.$el.append(v.render().el);

    });

   }

  });

  //begin play yard
   var tasks = new App.Collections.Tasks([
    {title : 'Go to Mall', priority : 4},
    {title : 'Go Home', priority : 3},
    {title : 'Go to Movie', priority : 2}
  ]);


  var tasksView = new App.Views.Tasks({collection : tasks});
  console.log(tasksView.el);

})();

1 个答案:

答案 0 :(得分:0)

以下是您的代码的a working jsfiddle

您的收藏视图已损坏。这是工作版本。

//collection view
// Notice that it's a `Backbone.View` and not a `Backbone.Collection`
App.Views.Tasks = Backbone.View.extend({

    tagName : 'ul',

    initialize : function(){this.render();},

    render : function(){

        this.collection.each(function(t){

            var v = new App.Views.Task({model : t});       

            this.$el.append(v.render().el);

        }, this); // scope to the view using `this`

    }

});