当使用 Bootstrap 3.0 和最终的 Ember 1.0 时,我无法让这段代码工作。
我重写了一下(在coffeescript中,布局和模板把手已经用Grunt的emberTemplates预编译为js)
app.coffee
App.ApplicationRoute = Ember.Route.extend({
actions:
open: ->
console.debug('open action triggered')
@render('ContactModal', {into: 'profile', outlet: 'contactModal'})
close: ->
@render('nothing', {into: 'profile', outlet: 'contactModal'})
save: ->
alert('Send the message to person')
})
modal_view.coffee
App.ModalView = Ember.View.extend({
didInsertElement: ->
@$('.modal').modal('show')
view = @
@$('.modal').on("hidden.bs.modal", (ev)->
view.controller.send('close')
return
)
layout: Ember.TEMPLATES['modal_layout']
template: Ember.TEMPLATES['modal']
actions:
close: ->
@$('.modal').modal('hide')
return
})
这种方式在模态外部单击也可以正确关闭它,因为从出口中删除模板是在隐藏模态时完成的。
modal.handlebars:
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" {{action close target="view"}}>×</button>
<h3 class="modal-title">Contact</h3>
</div>
<div class="modal-body">
<p>Here will go the contact form and contact template picker</p>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-default" {{action close target="view"}}>Close</a>
<a href="#" class="btn btn-primary" {{action save}}>Send</a>
</div>
</div>
modal_layout.handlebars
<div class="modal fade" role="dialog">{{yield}}<div>
我还放了一个 jsfiddle:http://jsfiddle.net/bG4F8/5/
玩得开心:)