2 Add events to a view
Nick Winter edited this page 2015-12-18 15:52:41 -08:00

Problem

You want your view to do something when the user clicks or otherwise fires a JavaScript/jQuery event.

Solution

Add or extend Backbone.View's events property. Be sure to follow CodeCombat's event naming guidelines.

Example

FooView.coffee

CocoView = require 'views/core/CocoView'
template = require 'templates/foo-view'

module.exports = class FooView extends CocoView
  id: 'foo-view'
  template: template
  events:
    'click #some-btn': 'onClickSomeButton'

  onClickSomeButton: (e) ->
    console.log 'Some button was clicked', e

foo-view.jade

button#some-btn.btn.btn-default Click Me!

Resources