开发者

Backbone View events won't fire

开发者 https://www.devze.com 2023-04-12 09:32 出处:网络
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):

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(co开发者_JAVA百科ntent: "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?


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

events:
    "click" : "test"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号