Apoorv Parijat
Apoorv Parijat

Reputation: 871

Why is .fetch() undefined for Collections?

I've my models and views for Backbone.js in separate files. The models and collections go like this

$(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"
});

});

The view code is:

$(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();
    },
        .
        .
        .
});

The javascript won't run now. It says the fetch() function is undefined. Any ideas?

Upvotes: 1

Views: 1294

Answers (1)

user920041
user920041

Reputation:

You should create a new instance of the Todos collection:

Todos = new TodoList.Collections.Todos();

Upvotes: 3

Related Questions