流星:在哪里放置$('#modal')。modal()调用以在许多路线上自动显示相同的模态?

时间:2015-06-17 14:25:52

标签: meteor twitter-bootstrap-3 modal-dialog iron-router

我正在使用Meteor开发一个应用程序。在几个页面上,我想在用户导航到该页面时以编程方式显示bootstrap-3模式。模态将给出如何使用相应页面的说明。用户可以禁用此行为。

阅读完这篇Meteor: execute jQuery code on every "change"后,我认为最好的办法就是在铁路由器的钩子里。

StepController = RouteController.extend({
  onAfterAction: function() {
    var userSettings = Meteor.user().profile.settings;
    if(userSettings.showHelp) {
      $('#modal').modal();
    }
  }
});

模态显示,但它使页面无法使用,因为模式不响应任何关闭它的尝试。

在评论中,我读到除了路由用户和相关的安全检查之外,不应该使用路由器。

因此,我在Stack Overflow上发现的所有建议位置都是模态调用:

  • 铁路由器挂钩
  • 模板助手
  • 模板渲染挂钩
老实说我很困惑。我非常感谢有关这方面的概念和最佳实践的教学。

1 个答案:

答案 0 :(得分:0)

如果在每次路径更改时打开模态的反应计算中使用Router.current()怎么样?

Template.myTemplate.onRendered(function () {
  var c = this.autorun(function () {
    Router.current();

    // Launch your modal here
    var userSettings = Meteor.user().profile.settings;
    if(userSettings.showHelp) {
      // Opens modal after template is updated
      Tracker.afterFlush(function () {
        $('#modal').modal();
      });
    }
  });

  // Close existing modal when route changes
  c.onInvalidate(function () {
    $('#modal').hide();
  });
});

如果此模式用于所有路线,您可以将Template.myTemplate更改为布局模板,甚至更改为Template.body

我在https://github.com/xamfoo/meteor-route-based-modal

中添加了一个工作示例