使用模态和 Iron Router 维护数据上下文?

maintaining the data context with a modal and Iron Router?

我正在使用 iron router 渲染模态,但想为模态所在的任何页面维护当前数据上下文,有没有办法让它触发一个动作但不杀死任何当前子/数据上下文 ?

Router.route('/box', {
  name: 'box',
  controller: 'AppController',
  action: function () {
    this.render('box', { to: 'modal' });
    $('.coverall').fadeIn(function() {
      $('.contain').addClass('blur');
    });
  }
});

所以总的来说这似乎是 iron router 中无法解决的问题,因为它总是会在导航到新路由时破坏数据上下文并结束订阅。

唯一可行的解​​决方案是真正不使用 Iron Router,将其替换为流路由器之类的东西,并在模板级订阅中管理订阅和数据上下文。

如果我理解这个问题,你可以使用 Blaze.renderWithData

例如,假设您要使用简单的 _id.

渲染模态框

所以你可以使用类似的东西。

Template.myNormalTemplate.events({
 'click .openModal':function(){
   var data = {
     _id:this._id
    };
   Blaze.renderWithData(Template.MyModalTemplate,data,document.body);
 }
});

然后在你的模态模板上使用这个。

<template name="myModalTemplate">
 <!-- Modal content here -->
</template>

Template.myModalTemplate.onRendered(function(){
  var _this = this;

  //this will open the modal when you call it with Blaze.renderWithData
  $(modalId).modal();

  console.log(_this.data)//this will print the data object you pass on the event
});

或者您可以在任何活动或助手中访问它们。

Template.myModalTemplate.helpers({
  myIdFromOtherView:function(){
    var instance = Template.instance();
    return instance.data._id
  }
 });

你懂的..