In Meteor How to copy data between templates? -
i'm new both meteor , js.
i created meteor app this:
cd /tmp/ meteor create hello cd hello
in hello.html wrote this
<body> <h1>welcome meteor!</h1> {{> t1}} {{> t2}} </body>
then added these templates:
<template name='t1'> <span id='t1'>hello</span> </template>
<template name='t2'> <span id='t2'>world</span> </template>
then wrote syntax inside of hello.js text dom:
template.t1.onrendered( function() { mytext = $('span#t1').html() });
according debugger-console in browser, above syntax works okay.
the debugger tells me mytext == 'hello'
question: how share mytext value other parts of meteor app?
as written, mytext stuck inside anonymous function.
for example how make call work:
$('span#t2').html(mytext)
??
you'll want reactively watch variable. easy out-of-the-box solution use session variable. can pretty so:
template.t1.onrendered( function() { session.set('mytext', $('span#t1').html()); });
then in second template, define , reference helper returns variable:
<!--html--> <template name='t2'> <span id='t2'>{{getmytext}}</span> </template> //js template.t2.helpers({ getmytext: function() { return session.get('mytext'); } });
the session variable reactive, when first template renders , sets value of session.mytext
, helper's result invalidated , second template update.
Comments
Post a Comment