Skip to content

Instantly share code, notes, and snippets.

@vishaltelangre
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishaltelangre/9070280 to your computer and use it in GitHub Desktop.
Save vishaltelangre/9070280 to your computer and use it in GitHub Desktop.
jquery on example
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.min.js"></script>
<div class="parent">
<div class="child">
<div class="widget"></div>
<button>Display Current Value</button>
</div>
<div>Current value of widget is: <span class="currentVal">N/A</div></div>
</div>
<script type="text/mustache" id="widgetTemplate">
<hr>
<h1>Blah Widget</h1>
<div>Current value of widget is: <span class="widgetVal">{{val}}</span></div>
<div>Rendered for {{times}} times.</div>
</script>
var widgetVal = 99999,
times = 0;
$('.parent').on('click', '.child button', function(e){
e.preventDefault();
var currentVal = $('.child .widgetVal').text();
$('.parent .currentVal').text( currentVal );
});
setInterval(function(){
var tmpl = $('#widgetTemplate').html();
widgetVal = widgetVal - 1; times = times + 1;
var compiledTmpl = Mustache.render(tmpl, {val: widgetVal, times: times });
$('.child .widget').html( compiledTmpl );
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment