为什么集合中的.fetch()未定义?

时间:2012-06-21 10:39:35

标签: javascript backbone.js

我在单独的文件中有Backbone.js的模型和视图。模型和集合就像这样

$(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()函数是未定义的。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您应该创建Todos集合的新实例:

Todos = new TodoList.Collections.Todos();