为什么不会'骨干火我的活动?
我的类定义如下:
class NewUser extends Backbone.View
template : JST["backbone/templates/events/new"]
events :
'click button' : 'hello'
hello : ->
alert('hi')
render : ->
@el = $(@template())
@
我按如下方式初始化页面:
$('body').append(new NewUser().render().el)
然而,当我单击按钮时,没有显示任何警报。有人看到我的代码有什么问题吗?视图呈现,但事件从未注册。
My class is defined as below:
class NewUser extends Backbone.View
template : JST["backbone/templates/events/new"]
events :
'click button' : 'hello'
hello : ->
alert('hi')
render : ->
@el = $(@template())
@
I initialize the page as follows:
$('body').append(new NewUser().render().el)
Yet when I click the button, no alert displays. Does anyone see anything wrong with my code? The view renders, but the events never register.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在
render
方法中创建@el
:但您忽略了调用
delegateEvents
:因此,您的事件永远不会绑定到您添加的
@el
到 DOM 并且你的按钮没有任何用处。您的渲染
应如下所示:You're creating your
@el
in yourrender
method:but you neglect to call
delegateEvents
:So, your events never get bound to the
@el
that you add to the DOM and your button does nothing useful. Yourrender
should look like this:按钮是渲染到视图的 el 的模板的一部分吗?
如果不是,则不会触发
hello
事件,因为事件哈希仅限于视图本身内的事件。在这种情况下,您需要手动绑定到render
方法中的事件Is the button part of the template which is rendered to the view's
el
?If it's not then the
hello
event won't be fired because the events hash is limited to events within the view itself. You will need to manually bind to the event in therender
method in that case