2014-02-20 76 views
0

我在主幹中運行這個示例程序。但是我得到一個錯誤,如'不能調用方法'得到'未定義'。我用迄今爲止所知道的所有方法都嘗試過。但我仍然有這個問題。任何人都可以幫助我解決這個問題。Uncaught TypeError:無法調用未定義的方法'get':BackBone

(function(){ 
    //Providing a global scope 
    window.App = { 
     Models : {}, 
     Views: {}, 
     Collections: {} 
    }; 

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

    // Declared a Model for Task  
    App.Models.Task = Backbone.Model.extend({  
    }); 

    App.Collections.Tasks = Backbone.Collection.extend({ 
     model: App.Models.Task 
    }); 

    App.Views.Tasks = Backbone.View.extend({ 
     tagName: 'ul', 

     render: function(){ 
      this.collection.each(this.addOne,this); 
     }, 

     addOne: function(task){ 
      //creating a child view 
      var taskView = new App.Views.Task({model: task}); 

      //append it to root element 
      this.$el.append(taskView.render().el); 
     } 
    });  

    App.Views.Task = Backbone.View.extend({ 
     tagName: 'li', 
     render : function(){ 
      this.$el.html(this.model.get('title')); 
      return this; 
     } 
    });   

    var tasksCollection = new App.Collections.Tasks([ 
     { 
     title: 'Goto Store', 
     priority: 4 
     }, 
     { 
     title: 'Goto Store2', 
     priority: 5 
     }, 
     { 
     title: 'Goto Store3', 
     priority: 6 
     }, 
     { 
     title: 'Goto Store4', 
     priority: 7 
     } 
    ]); 

    var tasksView = new App.Views.Task({collection : tasksCollection}); 
    tasksView.render(); 
    console.log(tasksView.el); 
    // $(document.body).append(tasksView.el);  
})(); 
+0

最後一行代碼是什麼'})();'? –

+1

這是一個立即調用的函數表達式。 – Puigcerber

回答

1

我不知道這是否是一個拼寫錯誤,但如果沒有,你的問題是,你要設置你的收藏在你的任務視圖,而不是在你的任務視圖。

var tasksView = new App.Views.Tasks({collection : tasksCollection}); 
相關問題