maintaining the data context with a modal and Iron Router?

157 views Asked by At

I'm rendering a modal with iron router, but want to maintain the current data context for whatever page the modal is on top of, is there a way to have it trigger an action but not kill any current subs / data context ?

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

There are 2 answers

0
bigmadwolf On BEST ANSWER

So all round this seems to be an unsolvable issue in iron router, as it will always destroy the data context and end subscriptions when navigating to a new route.

The only viable solution is to really not use Iron Router, substitute it for something like flow router, and manage subscriptions and data context in template level subscriptions.

1
Ethaan On

If im understanding this question you can use Blaze.renderWithData.

For example lets say you want to render a modal with the a simple _id.

So you can use something like.

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

And then on your Modal Template just use this.

<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
});

Or you can access them on any event or helper like this.

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

You get the idea..