1

Why am I getting the error: "Uncaught TypeError: Object [object Object] has no method 'create' "

I have tried to stay close to this backbone todo example code.

http://jsfiddle.net/GhaPF/4/

$(document).ready(function() {

    var ToDo = Backbone.Model.extend({
        defaults: { "date": "today",
                    "task": ""
                },
        initialize: function() {}
    });

    var ToDoList = Backbone.Model.extend({
        model: ToDo
    });

    var ToDoListView = Backbone.View.extend({
        el: 'body',
        initialize: function(myTodoList) {
            this.todolist = myTodoList;
            this.todolist.bind('change', this.render, this);
        },
        render: function() {
            text = this.todolist.toJSON();
            string = JSON.stringify(text);
            $(this.el).append(string);
            return this;
        },
        events: {
            "keypress #new-todo":  "createOnEnter"
        },
        createOnEnter: function(e) {
          if (e.keyCode != 13) return;
          if (!$("#new-todo").val()) return;
          this.todolist.create({"task": $("#new-todo").val()});
          $("#new-todo").val('');
        }
    });

    var todolist = new ToDoList();
    var myToDoListView = new ToDoListView(todolist);

});

I had two mistakes in there:

  1. defined ToDoList as a Model instead of a Collection
  2. used save() instead of add() on the collection, this triggered the hunt for a URL which did not exist

Here is a working code for what I wanted to achieve here:

$(document).ready(function() {

    var ToDo = Backbone.Model.extend({
        defaults: { "date": "today",
                    "task": ""
                },
        initialize: function() {}
    });

    var ToDoList = Backbone.Collection.extend({
        model: ToDo
    });

    var ToDoListView = Backbone.View.extend({
        el: 'body',
        initialize: function(myTodoList) {
            this.todolist = myTodoList;
            this.todolist.bind('add', this.render, this);
        },
        render: function() {
            text = this.todolist.toJSON();
            string = JSON.stringify(text);
            $(this.el).append(string);
            return this;
        },
        events: {
            "keypress #new-todo":  "createOnEnter"
        },
        createOnEnter: function(e) {
          if (e.keyCode != 13) return;
          if (!$("#new-todo").val()) return;
          this.todolist.add({"task": $("#new-todo").val()});
          //this.render();
          $("#new-todo").val('');
        }
    });

    var todolist = new ToDoList();
    var myToDoListView = new ToDoListView(todolist);

});

1 Answer 1

2

It looks like it is because your ToDoList is extending Backbone.Model. It should be extending Backbone.Collection.

Here's the code to fix it.

var ToDoList = Backbone.Collection.extend({
  model: ToDo,
  url: '/todos' 
}); 
Sign up to request clarification or add additional context in comments.

3 Comments

Ah correct! Thanks. But now I have another error: "Uncaught Error: A "url" property or function must be specified". jsfiddle.net/GhaPF/5
Since the create function saves the model to the server, you have to add the url propert to the collection so it knows what url to call on the server to add a model. See the url documentation: backbonejs.org/#Collection-url
Thank you! I confused save() with add().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.