2012-06-21 68 views
1

我有我的模型和意见Backbone.js在单独的文件。该模型和集合是这样的为什么.fetch()未定义为Collections?

$(function(){ 

    TodoList.Models.Todo = Backbone.Model.extend({ 
     url: function(){ 
       return this.id? '/todos/' + this.id : '/todos/' ; 
       }, 
     initialize: function() { 
       } 
    }); 

    TodoList.Collections.Todos = Backbone.Collection.extend({ 
     model: TodoList.Models.Todo, 
     url: "/todos" 
}); 

}); 

视图代码是:

$(function(){ 
    Todos = TodoList.Collections.Todos; 
    TodoList.Views.AppView = Backbone.View.extend({ 
    el: $("#todo_app"), 
    events: { 
     "submit form#new_todo": "createTodo" 
    }, 
    initialize: function(){ 
     _.bindAll(this, 'addOne', 'addAll'); 
     Todos.bind("add", this.addOne); 
     Todos.bind("refresh", this.addAll); 
     Todos.bind("all", this.render); 
     Todos.fetch(); 
    }, 
     . 
     . 
     . 
}); 

的JavaScript现在不会运行。它说fetch()函数是未定义的。有任何想法吗?

回答

3

您应该创建托多斯收集的新实例:

Todos = new TodoList.Collections.Todos(); 
+0

在本教程中, https://github.com/AndrewGertig/backbone-demo/blob/master/public/javascripts/dogs。 js 在AppView代码块中,没有为Dog集合创建新对象并使用提取。 –

+0

我的不好。现在明白了。万分感谢。 –

+0

是的,你有'window.Dogs = new DogCollection;'在线28 – 2012-06-21 10:49:48