我已将我的应用程序设置为使用ember路由架构。我的索引页面看起来像这样(为简单起见)
script(type='text/x-handlebars', data-template-name='application')
div.container
{{outlet}}
和我的ember应用程序一样
window.App = Em.Application.create({
ApplicationController: Em.Controller.extend(),
ApplicationView: Ember.View.extend({
templateName: 'application'
}),
Router: Em.Router.extend({
root: Em.Route.extend({
doHome: (router, event) ->
router.transitionTo('home')
doInbox: (router, event) ->
router.transitionTo('inbox')
doInboxModal: (router, event) ->
$("#inbox").modal "show"
home: Em.Route.extend({
route: '/',
connectOutlets: (router, event) ->
router.get('applicationController').connectOutlet('home')
}),
inbox:Em.Route.extend({
route: '/inbox',
connectOutlets: (router, event) ->
router.get('applicationController').connectOutlet('inbox')
})
})
})
我的家和收件箱工作正常,但首先,我在doInboxModal中做jquery来显示模态收件箱。如果我想在模态收件箱上有一个按钮转到实际的收件箱页面,它将无效。
所以,问题是,如何正确使用带有ember路由的Twitter Bootstrap Modal?
答案 0 :(得分:7)
当您路由到视图时,请调用didInsertElement中的模态,该模式将加载模态。假设您希望在收件箱视图中加载模态
App.InboxView = Ember.View.extend({
didInsertElement: function(){
$("#my-modal").modal("show");
}
})
您更新的路由器:
window.App = Em.Application.create({
ApplicationController: Em.Controller.extend(),
ApplicationView: Ember.View.extend({
templateName: 'application'
}),
Router: Em.Router.extend({
root: Em.Route.extend({
doHome: (router, event) ->
router.transitionTo('home')
doInbox: (router, event) ->
router.transitionTo('inbox')
home: Em.Route.extend({
route: '/',
connectOutlets: (router, event) ->
router.get('applicationController').connectOutlet('home')
}),
inbox:Em.Route.extend({
route: '/inbox',
connectOutlets: (router, event) ->
router.get('applicationController').connectOutlet('inbox')
})
})
})
希望这会有所帮助......
Updated Answer
App.InboxView = Ember.View.extend({
templateName: "inbox",
addNewEmail: function(){
$("#my-modal").modal("show");
},
cancelNewEmail: function(){
$("#my-modal").modal("hide");
}
})
inbox.handlebars
<div id="inbox-container">
<!--
YOUR INBOX CONTENT
The modal declared below wont show up unless invoked
-->
<a {{action addNewEmail}}>New Email</a>
<a {{action cancelNewEmail}}>Cancel</a>
<div class="modal hide fade in" id="my-modal">
<!--
Put your modal content
-->
</div>
</div>
这样:
答案 1 :(得分:5)
如果有人想进一步参考,我已经发布了使用Twitter Bootstrap模式和Ember.js:
http://generali.st/en/site/topics/show/82-modal-forms-in-emberjs
包含完整源代码的工作JSBin。还有一些干燥表格的策略。 IMO,事物的命名也有点传统。
答案 2 :(得分:4)
我花了一些时间来剖析话语,以了解他们是如何做到这一点的。基本上他们有一个单例模态,路由器处理事件以显示和隐藏模态。
以下是有趣的内容:
话语/应用/资产/ Javascript角/话语/路由/ discourse_route.js
showModal: function(router, name, model) {
router.controllerFor('modal').set('modalClass', null);
router.render(name, {into: 'modal', outlet: 'modalBody'});
var controller = router.controllerFor(name);
if (controller) {
if (model) {
controller.set('model', model);
}
controller.set('flashMessage', null);
}
}
话语/应用/资产/ Javascript角/话语/路由/ application_route.js
events: {
editCategory: function(category) {
Discourse.Route.showModal(router, 'editCategory', category);
router.controllerFor('editCategory').set('selectedTab', 'general');
}
话语/应用/资产/ Javascript角/话语/视图/模态/ modal_body_view.js
Discourse.ModalBodyView = Discourse.View.extend({
// Focus on first element
didInsertElement: function() {
$('#discourse-modal').modal('show');
var controller = this.get('controller');
$('#discourse-modal').on('hide.discourse', function() {
controller.send('closeModal');
});
$('#modal-alert').hide();
var modalBodyView = this;
Em.run.schedule('afterRender', function() {
modalBodyView.$('input:first').focus();
});
var title = this.get('title');
if (title) {
this.set('controller.controllers.modal.title', title);
}
},
willDestroyElement: function() {
$('#discourse-modal').off('hide.discourse');
}
});
话语/ app / assets / javascripts /话语/ mixins / modal_functionality.js
Discourse.ModalFunctionality = Em.Mixin.create({
needs: ['modal'],
/**
Flash a message at the top of the modal
@method blank
@param {String} name the name of the property we want to check
@return {Boolean}
**/
flash: function(message, messageClass) {
this.set('flashMessage', Em.Object.create({
message: message,
messageClass: messageClass
}));
}
});
应用程序/资产/ Javascript角/话语/模板/模态/ modal.js.handlebars
<div class="modal-header">
<a class="close" {{action closeModal}}><i class='icon-remove icon'></i></a>
<h3>{{title}}</h3>
</div>
<div id='modal-alert'></div>
{{outlet modalBody}}
{{#each errors}}
<div class="alert alert-error">
<button class="close" data-dismiss="alert">×</button>
{{this}}
</div>
{{/each}}
在他们的application.js.handlebars中:{{render modal}}