Interaction between template instances

Below is a simplified scenario:

I have Two different templates, one is Main template and other is sidebar template.

The sidebar template keeps tracks of user interaction history and needs to access the instance variables of the main template - how to get them?

I do not like the idea of using global variables for interaction among template instances - is there no “official” way of template instance communication in meteor apart from global vars?

For e.g., main template has instance collections that need be accessed through sidebar.

I guess sidebar is a child of Main. In that case, you can use this:

// Returns an ancestor instance of this template instance (by name)
Blaze.TemplateInstance.prototype.parentInstance = function (templateName) {
  	if (! /^Template\./.test(templateName))
    		templateName = 'Template.' + templateName;
  	var view = this.view;
  	while (view = view.parentView)
    		if (view.name === templateName)
      			return view.templateInstance();
};


Template.sidebar.created = function() {
  var mainCollection = this.parentInstance('Main').myCollection;
  ...
};

Full discussion here. You might also want to have a look at aldeed:template-extension.

1 Like

Thanks @Steve Looks like aldeed: template-extension is promising.