Search code examples
javascriptjquerybackbone.jsbackbone-events

Backbone View events won't fire


I am using Backbone in a project and I am trying to get the events functionality of Backbone Views to work correctly. I have the following code (it is extracted out of my current application):

My Base File:

window.App =
   Models: {}
   Views: {}

My Model:

class App.Models.Story extends Backbone.Model
  defaults:
    id: null
    content: null

My View:

class App.Views.Story extends Backbone.View
  tagName: "li"
  className : "story item"

  events:
    "click" : "test"

  test: ->
    alert "TEST"

  render: ->
    html = "<div> #{@model.get("content")} </div>"
    $(@el).html(html)
    return this

The Code on page load:

$(document).ready ->
  story = new App.Models.Story(content: "Some content")
  view = new App.Views.Story(model: story)
  $("body").append(view.render().el)

The element renders but when I click on it, the click handler does not execute. What am I doing wrong?


Solution

  • You are going to hate this answer... you need to declare events not event:

    events:
        "click" : "test"