1

我将 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() 函数未定义。有任何想法吗?

4

1 回答 1

3

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

Todos = new TodoList.Collections.Todos();
于 2012-06-21T10:43:42.267 回答