How to send data from one template to other in Meteor

Posted By : Parveen Kumar Yadav | 14-Jul-2016

Sometimes we have requirement for using result of one template into other template in Meteor and this is very common thing to use.

So in this blog I am going to explain how you can use one template data in other template.

Suppose you have a template name allInventory on which click event you get some data like id and you  will go to some other template named as productDetail with that data and now you want to show or render that data on productDetail html template.

lets take an situation like:-

allInventory.js


 Template.allInventory.rendered = function() {
Template.allInventory.events({
      "click .btn":function (e){
       data  = $(e.target).attr('data');
    Router.go('productDetail', {data: $(e.target).attr('data')}, {query: 'q=s', hash: 'hashFrag'});
    console.log(data);
  }
})

productDetails.js


 Template.productDetail.rendered = function () {
Template.productDetail.helpers({
 productDetails: function() {  
      return data;
}
    });
 

allInvenrtory.html


 

I just simply want to share allInventory template data with productsDetails template.

So here i need to use allInventory.js template data in productDetails.js template so to achieve this one approach is using Session package

 install that via:-

meteor add session

But there is one problem with this session this will not persists your values as you refresh the page so for that solution you can use:-

u2622:persistent-session

To add simple write:-

meteor add u2622:persistent-session

now there are mainly four methods of this you can use according to your need and if you want your data will be persist after refresh as well than use

Session.setPersistent(key, value)

So we can set our data as get from allInventory and get it in productDetailspage like:-

allinventory.js

Template.allInventory.rendered = function() {

Template.allInventory.events({
      "click .btn":function (e){
       data  = $(e.target).attr('data');
	Session.setPersistent("data", data)
    Router.go('productDetail', {data: $(e.target).attr('data')}, {query: 'q=s', hash: 'hashFrag'});
    console.log(data);
  }
})

productsDetails.js

Template.productDetail.rendered = function () {
Template.productDetail.helpers({
 productDetails: function() {  
var data = Session.get(data);
      return data;
}
    });

So in this way you can share data between multiple templates.

Here is my stackoverflow link for same question:-

http://stackoverflow.com/questions/37937964/how-to-send-some-data-of-one-template-to-another-template-meteor

Thanks!

About Author

Author Image
Parveen Kumar Yadav

Parveen is an experienced Java Developer working on Java, J2EE, Spring, Hibernate, Grails ,Node.js,Meteor,Blaze, Neo4j, MongoDB, Wowza Streaming Server,FFMPEG,Video transcoding,Amazon web services, AngularJs, javascript. He likes to learn new technologies

Request for Proposal

Name is required

Comment is required

Sending message..