P
P
Pavel Zinoviev2015-05-19 18:47:50
JavaScript
Pavel Zinoviev, 2015-05-19 18:47:50

How to set up Backbone view events?

Good evening. Essence of the question:
There is a project using backbone, underscore, browserfy and coffeescript
There is a view in this project:

"use strict"

_             = require "underscore"
$             = require "jquery"
Backbone      = require "backbone"

Works_tpl     = require "modules/works/templates/works_template.coffee"

module.exports = Backbone.View.extend

  el      : ".works-wrapper"
  template: _.template Works_tpl

  events  : {
    "click .change-view__item": "changeView"
  }

  initialize: (works, type = "list")
    @render works, type
    return

  render: (works, type) ->
    @$el.html @template
    return

  changeView: ->
    console.log 123
    return

There is a template (underscore templates) in this project:
"use strict"

module.exports = '
<div class="change-view__item">Нажми на меня</div>
'

Renders everything fine, but events (view events) do not work. What needs to be done to make it work? Preferably with an example on my code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2015-05-20
@k12th

You need to call @delegateEvents at some point (preferably after @el has content.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question