Backbone.js / Coffeescript -- 映射简单的点击事件
我遇到了backbone.js 的盲点。我已经写了这段代码,我想做的就是得到 单击按钮时要渲染的测试 div。如果我在 initialize()
函数中调用 render()
,它就会进行渲染。 但是,我从未触发 render()
函数来响应单击事件。
我缺少什么?
谢谢!
用于简单视图的HTML 容器
<!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
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'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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
马吕斯说的是对的。
通常,您希望在类定义中而不是在初始值设定项中指定
事件
。事实上,对于这个示例,您根本不需要initialize
函数:What Marius said is right.
Typically you'd want to specify the
events
in the class definition, not in an initializer. In fact, for this example you wouldn't need aninitialize
function at all:我认为在
initialize
被调用之前,在View
构造函数中调用设置事件处理的delegateEvents
,所以要么在您的initialize
方法(在设置events
映射之后),或者确保将events
映射设置为构建查看
类本身,请参阅:http://documentcloud.github.com/backbone/#View-extend
I think
delegateEvents
, which sets up the event handling, gets called in theView
constructor, beforeinitialize
gets called, so either call it explicitely in yourinitialize
method (after you have set up theevents
map), or make sure that theevents
map is set up as part of constructing theView
class itself, see:http://documentcloud.github.com/backbone/#View-extend