Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created March 6, 2014 16:43
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 krisleech/9393896 to your computer and use it in GitHub Desktop.
Save krisleech/9393896 to your computer and use it in GitHub Desktop.
Instead of javascript templates reuse the DOM as a template
<div id="results">
<div class="result" data-result-id='1'>
<div data-attr="title">Result 1</div>
</div>
<div class="result" data-result-id='2'>
<div data-attr="title">Result 2</div>
</div>
<div class="result" data-result-id='3'>
<div data-attr="title">Result 3</div>
</div>
</div>
// pseudo code
$.getJSON('/search', { :query => 'foo' }, function(json) {
template = $('#results .result').first().clone();
$('#results .result').remove();
_.each(json['results'], function(result) {
result_html = template.clone()
result_html.find('[data-attr=title]').text(result['title']);
$('#results').append(result_html);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment