Skip to content

Instantly share code, notes, and snippets.

@tamueka
Created August 28, 2018 17:45
Show Gist options
  • Save tamueka/7a73a7d74a77efb2f84314a777bb80e1 to your computer and use it in GitHub Desktop.
Save tamueka/7a73a7d74a77efb2f84314a777bb80e1 to your computer and use it in GitHub Desktop.
jQuery View - TODO List
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header">Todos</h1>
<ul class="todo-list">
<li :repeat="todo in todos">
<strong :class="{todoDone: todo.done}">
{{ todo.name }}
</strong>
<div class="pull-right">
<button class="btn btn-sm btn-success" :click="this.toggle(todo)">
<span class="glyphicon glyphicon-ok"></span>
</button>
<button class="btn btn-sm btn-danger" :click="this.remove(todo)">
<span class="glyphicon glyphicon-remove"></span>
</button>
</div>
</li>
</ul>
<form class="todo-form" :submit="this.submitHandler($event)">
<div class="form-group">
<input type="text" class="form-control" placeholder="Add new TODO..." :data="todo">
</div>
</form>
</div>
</div>
</div>
var defaultTodos = [
{name: 'Improve jQuery View'},
{name: 'Some other task'},
{name: 'Bla Bla Bla', done: true}
]
var $$todoList = $('.todo-list').view({
state: {
todos: defaultTodos
},
add: function (todo) {
this.insertIntoState('todos', {
name: todo,
done: false
})
},
remove: function (todo) {
this.removeFromState('todos', todo)
},
toggle: function (todo) {
this.extendStateItem('todos', todo, {done: !todo.done})
}
})
var $$todoForm = $('.todo-form').view({
submitHandler: function (e) {
e.preventDefault()
$$todoList.add(this.data.todo)
this.data.todo = ''
this.data.$todo.focus()
}
})
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://codepen.io/gsantiago/pen/eZEwMJ"></script>
.todo-list
padding 0
list-style-position inside
li
overflow auto
margin-bottom 15px
.todo-form
margin-top 50px
.todo-done
text-decoration line-through
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment