使用 bootstrap-modal 作为 Backbone.js 视图
我正在尝试创建一个基于 Twitter 引导模式的 Backbone.js 视图,该视图通过视图的 events
属性利用 Backbone 的自动事件委托。
不幸的是,bootstrap-modal 似乎破坏了 Backbone 的事件委托,因为它在显示模式之前克隆了视图 HTML:
that.$element
.appendTo(document.body)
.show()
我的视图:
App.Views.ProjectsNav ||= {}
class App.Views.ProjectsNav.NewProjectView extends Backbone.View
events: {
'click .save': 'save',
'shown': 'shown'
}
save: (e) ->
...
false
shown: () ->
App.Helpers.Forms.setFocus($(@el), true)
false
render: () ->
$(@el).html(ich.nav_edit_project_template(@model.toJSON()))
@$('.modal').modal({'show': true, 'keyboard': true, 'backdrop': true})
@
相应的(Mustache)HTML 模板:
<div class="modal hide" style="display: none; ">
<div class="modal-header">
<a href="#" class="close">×</a>
<h3>New Project</h3>
</div>
<div class="modal-body form-stacked">
<label for="name">Name</label> <input type="text" name="name" value="{{name}}"/><input type="hidden" name="lock_version" value="{{lock_version}}"/>
</div>
<div class="modal-footer">
<a href="javascript:void(0)" class="save btn primary">Create</a>
<a href="javascript:void(0)" class="cancel btn secondary">Cancel</a>
</div>
</div>
既不 save
也不 show
在触发相应事件时调用。
有什么想法吗?
I am attempting to create a Backbone.js view based on a Twitter bootstrap-modal, which makes use of Backbone's automatic event delegation via the events
attribute of the view.
Unfortunately, bootstrap-modal seems to break Backbone's event delegation as it clones the view HTML before displaying the modal:
that.$element
.appendTo(document.body)
.show()
My view:
App.Views.ProjectsNav ||= {}
class App.Views.ProjectsNav.NewProjectView extends Backbone.View
events: {
'click .save': 'save',
'shown': 'shown'
}
save: (e) ->
...
false
shown: () ->
App.Helpers.Forms.setFocus($(@el), true)
false
render: () ->
$(@el).html(ich.nav_edit_project_template(@model.toJSON()))
@$('.modal').modal({'show': true, 'keyboard': true, 'backdrop': true})
@
The corresponding (Mustache) HTML template:
<div class="modal hide" style="display: none; ">
<div class="modal-header">
<a href="#" class="close">×</a>
<h3>New Project</h3>
</div>
<div class="modal-body form-stacked">
<label for="name">Name</label> <input type="text" name="name" value="{{name}}"/><input type="hidden" name="lock_version" value="{{lock_version}}"/>
</div>
<div class="modal-footer">
<a href="javascript:void(0)" class="save btn primary">Create</a>
<a href="javascript:void(0)" class="cancel btn secondary">Cancel</a>
</div>
</div>
Neither save
nor shown
are called when the respective events are triggered.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,所以解决方案相当简单:
总结一下,关键是将显示模式分为两个步骤,从而可以分配
@el
和调用@delegateEvents() 之后才使其可见。
@el.modal(true)
可用于访问控制模态的对象,例如以编程方式隐藏它。Allright, so the solution is was rather simple:
Summing things up, the key is to split showing the modal into two steps giving the possibility to assign
@el
and invoke@delegateEvents()
afterwards before making it visible.@el.modal(true)
can be used to get access to the object controlling the modal, e.g., to programmatically hide it.