Last active
August 29, 2015 14:12
-
-
Save plusjade/92b5287f7fe23c865008 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<form> | |
<input name="item"> | |
<button type="submit">Add Item</button> | |
</form> | |
<ul id="todo-items"> | |
</ul> | |
<script id="item-template" type="text/x-template"> | |
<li> | |
<span>{{ name }}</span> | |
<img src="some-image"> | |
<button class="remove">remove</button> | |
</li> | |
</script> | |
<script> | |
var parsedUserData = { | |
"name" : "John Doe", | |
"items" : [ | |
"Item 1", | |
"Item 2" | |
"Item 3" | |
] | |
} | |
// Build the list elements dynamically from data using a template. | |
// Caching is important here because appending elements into the DOM is expensive. | |
var cache = ''; | |
parsedUserData.items.forEach(function(item) { | |
cache += Mustache.render($("#item-template").html(), { name: item }); | |
}) | |
$("todo-items").append(cache); | |
// Activate the "remove" buttons. | |
$("todo-items").on('click', 'button.remove', function() { | |
$(this).parent().remove(); | |
}) | |
$('form').submit(function(e) { | |
e.preventDefault(); | |
var content = $('input').val()); | |
$("todo-items").append($('<li></li>').text(content)); | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment