Skip to content

Instantly share code, notes, and snippets.

@kenwheeler
Last active August 29, 2015 14:01
Show Gist options
  • Save kenwheeler/e2850b772d3358eced96 to your computer and use it in GitHub Desktop.
Save kenwheeler/e2850b772d3358eced96 to your computer and use it in GitHub Desktop.
Persistent TODO's with Object.observe

Setup

bower install jquery bootstrap

(function($, app){
function app() {
window.myArray = JSON.parse(localStorage.getItem('todos')) || [];
app.populateList(window.myArray);
app.bindEvents();
}
app.bindEvents = function() {
Object.observe(window.myArray, function(){
app.populateList(window.myArray);
});
$('form').on('submit', function(e){
e.preventDefault();
if($('#bitch').val() != '') {
window.myArray.push($('#bitch').val());
localStorage.setItem('todos', JSON.stringify(window.myArray));
$('#bitch').val('');
} else {
alert('Fill it the fuck out bro.')
}
});
$('.list-group').on('click', '.list-group-item a.pull-right', function(){
var theIndex = $(this).parent().index();
window.myArray.splice(theIndex, 1);
localStorage.setItem('todos', JSON.stringify(window.myArray));
});
};
app.populateList = function(arr) {
var listString = '';
for(var i = 0; i < arr.length; i++) {
listString += '<li class="list-group-item"><span class="todo">' + arr[i] + '</span><a href="javascript:void(0)" class="pull-right">delete</a></li>';
}
$('.list-group').html(listString);
};
return app();
})(jQuery, window.app = window.app || {});
<html>
<head>
<title>Bitches</title>
<link rel="stylesheet" type="text/css" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12 clearfix">
<h1>Todos</h1>
<form class="form-inline pull-right">
<div class="form-group">
<input type="text" class="form-control" id="bitch"/>
<input type="submit" class="form-control" value="Add Todo"/>
</div>
</form>
</div>
<div class="col-lg-12 clearfix">
<ul class="list-group">
</ul>
</div>
</div>
</div>
<script type="text/javascript" src="bower_components/jquery/dist/jquery.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
.container {
background: #efefef;
}
h1 {
display: inline-block;
font-size: 24px;
margin: 12px;
}
span.todo {
display: inline-block;
width: 50%;
}
form {
margin: 12px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment