owencm
owencm

Reputation: 8874

Backbone collection.create not triggering 'add' event

I'm struggling with a simple backbone/coffeescript example. I'm trying to add a Question to the QuestionList and have it fire an 'add' event on the collection so I can render it. I'm using the create method since I am trying to have it connect to my server, here modelled by the console.

In this example the console prints "create: {"question":"Question","answer":"Answer"}" but not "Event occurred" as expected. What am I doing wrong here?

jQuery ->
    class Question extends Backbone.Model
        defaults:
            question: 'Question'
            answer: 'Answer'

    class QuestionList extends Backbone.Collection
        model: Question

        initialize: ->
            @bind 'all', -> console.log "Event occurred"

    Backbone.sync = (method, model) ->
        console.log method + ": " + JSON.stringify(model)

    question_list = new QuestionList

    question_list.fetch()

    question_list.create 
                    question: $('#question').val()
                    answer: $('#answer').val()

Upvotes: 0

Views: 324

Answers (1)

owencm
owencm

Reputation: 8874

The version of Backbone being used was old since I'd downloaded a tutorial and continued working from there. Upgrading to Backbone 0.9.9 solved the issue. (thanks Fencliff!)

Upvotes: 1

Related Questions