I’m running into a blind spot with backbone.js. I’ve written this code and all I’m trying to do is get
the test div to render on button click. If I call render() within the initialize() function, it does render.
However, I never trigger the render() function in response to the click event.
What am I missing?
Thanks!
HTML Container
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<!-- / Always force latest IE rendering engine (even in intranet) & Chrome Frame -->
<meta content='IE=edge,chrome=1' />
<meta http-equiv='X-UA-Compatible' />
<link type="text/css" rel="stylesheet" href="/stylesheets/site.css" media="screen" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="/javascripts/underscore-min.js"></script>
<script type="text/javascript" src="/javascripts/backbone-min.js"></script>
<script type="text/javascript" src="/javascripts/application.js"></script>
<script type="text/javascript" src="/javascripts/sample_data.js"></script>
<title>The Middleman!</title>
</head>
<body class='page_classes'>
<div id='main' role='main'>
<h1>The Middleman is watching.</h1>
<div id='band-app'>
<button id='new-band'>new band</button>
</div>
</div>
</body>
</html>
Coffeescript for simple view
SAMPLE_TEMPLATE = """
<div id="my-fine-id">
My fine id!!!
</div>
"""
$ ->
class AppView extends Backbone.View
render: ->
console.log "rendering"
@template = _.template SAMPLE_TEMPLATE, {}
@el.html @template
initialize: ->
console.log "constructing"
@el = $('#band-app')
@events = {"click button#new-band": "render"}
app = new AppView()
I think
delegateEvents, which sets up the event handling, gets called in theViewconstructor, beforeinitializegets called, so either call it explicitely in yourinitializemethod (after you have set up theeventsmap), or make sure that theeventsmap is set up as part of constructing theViewclass itself, see:http://documentcloud.github.com/backbone/#View-extend