Meteor将值从模式单击事件传递到父模板



我正在尝试在Meteor中制作一个可重复使用的确认模式。我有模态工作的意义上,它打开并显示内容。我的想法是,我可以在任何地方使用它,如果用户单击确认,它将返回一个值,然后我可以运行我选择的任何服务器端方法。确认模板js很简单,看起来像这样:

Template.confirm.events({
  'click .confirm': function() {
    // I want to let the template that opened the modal know the user has confirmed. How do I pass this information?
  },
  'click .cancel': function() {
    //Close the modal
  }
});

我知道有一些解决方案,例如甜蜜警报,但我正在使用Zurb Foundation作为我的UI,并希望将所有内容都保持在这个框架内,以实现一致性和控制。有人能帮我指明正确的方向吗?

非常感谢。

没有标准的方法来实现这一点,但我建议将您在模态之外观察到的reactiveVar传递给模态,并在reactiveVar更改为您想要的值时触发服务器端方法。一些骨架代码可能看起来像:

html

{{> confirm triggerVar=myReactiveVar}}

模态js

Template.confirm.events({
  'click .confirm': function() {
    this.triggerVar.set(true);
  },
  'click .cancel': function() {
    this.triggerVar.set(false);
  }
});

在你的控制器js

Template.someTemplate.onCreated(function() {
  this.myReactiveVar = new ReactiveVar();
})
Template.someTemplate.onRendered(function() {
    // observe changes on your fancy reactiveVar
    this.autorun(() => {
        if (this.myReactiveVar.get() === true) {
            // fire whatever server-side action you want
        }
    })
})

最新更新