2014-02-26 113 views
0

我正在运行此示例主干脚本。但是我得到一个错误,如'不能调用方法'得到'未定义'。我用迄今为止所知道的所有方法都尝试过。但我仍然有这个问题。任何人都可以帮助我解决这个问题。骨干:未捕获TypeError:无法调用未定义的方法'each'

// 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); 

})(); 

回答

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` 

    } 

}); 
相关问题