【发布时间】:2014-03-15 01:51:49
【问题描述】:
我有以下模板:
<template name="modalTest">
{{session "modalTestNumber"}} <button id="modalTestIncrement">Increment</button>
</template>
session 助手只是Session 对象的中间人。我将 modalTestNumber 初始化为 0。
我希望将此模板连同其所有反应性一起呈现到引导箱模式对话框中。我为此模板声明了以下事件处理程序:
Template.modalTest.events({
'click #modalTestIncrement': function(e, t) {
console.log('click');
Session.set('modalTestNumber', Session.get('modalTestNumber') + 1);
}
});
以下是我尝试过的所有方法,以及它们的结果:
bootbox.dialog({
message: Template.modalTest()
});
这会渲染模板,它看起来或多或少像0 Increment (in a button)。但是,当我从控制台更改 Session 变量时,它不会更改,并且当我单击按钮时不会调用事件处理程序(console.log 甚至不会发生)。
message: Meteor.render(Template.modalTest())
message: Meteor.render(function() { return Template.modalTest(); })
这两者的作用与 Template 调用本身的作用完全相同。
message: new Handlebars.SafeString(Template.modalTest())
这只是将模态体呈现为空。模态仍然会弹出。
message: Meteor.render(new Handlebars.SafeString(Template.modalTest()))
与Template 和纯Meteor.render 调用完全相同;模板在那里,但它没有反应或事件响应。
是不是我使用的是this less packaging of bootstrap 而不是标准包?
我怎样才能让它以适当的反应 Meteor 风格呈现?
入侵 Bootbox?
我刚刚尝试入侵bootbox.js 文件本身,看看我是否可以接管。我改变了一些东西,以便在bootbox.dialog({}) 层我只需传递我想要渲染的模板的名称:
// in bootbox.js::exports.dialog
console.log(options.message); // I'm passing the template name now, so this yields 'modalTest'
body.find(".bootbox-body").html(Meteor.render(Template[options.message]));
body.find(".bootbox-body").html(Meteor.render(function() { return Template[options.message](); }));
这两个不同的版本(不要担心它们是两种不同的尝试,而不是同时)它们都以非反应性方式呈现模板,就像它们之前所做的那样。
入侵 bootbox 会有什么不同吗?
提前致谢!
【问题讨论】:
-
虽然不是 bootbox,但Crater 具有旨在与 Meteor 一起使用的叠加层。
-
这取决于您使用的 Meteor、Bootstrap 和 Bootbox 的版本。 Bootbox 4.0.0 及更高版本仅与 Bootstrap 3 兼容,并且 Meteor 0.8 及更高版本的渲染方式不同。
标签: meteor modal-dialog bootbox