Created
March 6, 2014 16:43
-
-
Save krisleech/9393896 to your computer and use it in GitHub Desktop.
Instead of javascript templates reuse the DOM as a template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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