简体   繁体   中英

Backbone events not firing

I know other posts have been made regarding this, but so far the answers I've seen have not been helpful, and slightly different from my situation.

window.BotView = Backbone.View.extend
  initialize: ->
    _.bindAll @, 'alert', 'render'
    @el # by calling this here, it initializes the jQuery object

  el: $("#submit")

  model: Chatbot

  events:
    "click #submit" : "alert"

  alert: ->
    console.log("alert called")
    alert("event observed")

  render: ->
    alert("Rendered")


jQuery ->
  window.App = new BotView
  console.log App.el

All I want is when I click on the submit button with the id of submit for it to fire the alert function. However, I can't even get this to work.

What is going on with the events that my simple click handler on #submit isn't working?

I have double checked that my el is properly initialized, but even so, it should not matter because the click handler is not using el

Could anyone shed some light on why this simple event is not firing?

Thanks in advance

In your events you are saying, in the #submit element, look for an element that gets clicked with the ID of #submit. Change it to

'click' : 'alert'

and it should work fine.

The jQuery equivalent of what you have above is this:

$('#submit').find('#submit').click(alert);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM