Commit 1f2793f1 authored by Sindre Sorhus's avatar Sindre Sorhus

jQuery: Use templating on the footer too

parent 8e1adcf4
#main,
#footer {
display: none;
}
\ No newline at end of file
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>jQuery • TodoMVC</title> <title>jQuery • TodoMVC</title>
<link rel="stylesheet" href="../../assets/base.css"> <link rel="stylesheet" href="../../assets/base.css">
<link rel="stylesheet" href="css/app.css">
<!--[if IE]> <!--[if IE]>
<script src="../../assets/ie.js"></script> <script src="../../assets/ie.js"></script>
<![endif]--> <![endif]-->
...@@ -30,7 +31,7 @@ ...@@ -30,7 +31,7 @@
<p>App and template by <a href="http://github.com/sindresorhus">Sindre Sorhus</a></p> <p>App and template by <a href="http://github.com/sindresorhus">Sindre Sorhus</a></p>
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p> <p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
</footer> </footer>
<script type="text/x-handlebars-template" id="todo-template"> <script id="todo-template" type="text/x-handlebars-template">
{{#this}} {{#this}}
<li {{#if completed}}class="completed"{{/if}} data-id="{{id}}"> <li {{#if completed}}class="completed"{{/if}} data-id="{{id}}">
<div class="view"> <div class="view">
...@@ -42,6 +43,10 @@ ...@@ -42,6 +43,10 @@
</li> </li>
{{/this}} {{/this}}
</script> </script>
<script id="footer-template" type="text/x-handlebars-template">
<span id="todo-count"><strong>{{activeTodoCount}}</strong> {{activeTodoWord}} left</span>
{{#if completedTodos}}<button id="clear-completed">Clear completed ({{completedTodos}})</button>{{/if}}
</script>
<script src="../../assets/base.js"></script> <script src="../../assets/base.js"></script>
<script src="../../assets/jquery.min.js"></script> <script src="../../assets/jquery.min.js"></script>
<script src="../../assets/handlebars.min.js"></script> <script src="../../assets/handlebars.min.js"></script>
......
...@@ -19,7 +19,8 @@ jQuery(function( $ ) { ...@@ -19,7 +19,8 @@ jQuery(function( $ ) {
this.render(); this.render();
}, },
cacheElements: function() { cacheElements: function() {
this.template = Handlebars.compile( $('#todo-template').html() ); this.todoTemplate = Handlebars.compile( $('#todo-template').html() );
this.footerTemplate = Handlebars.compile( $('#footer-template').html() );
this.$todoApp = $('#todoapp'); this.$todoApp = $('#todoapp');
this.$newTodo = $('#new-todo'); this.$newTodo = $('#new-todo');
this.$toggleAll = $('#toggle-all'); this.$toggleAll = $('#toggle-all');
...@@ -41,7 +42,7 @@ jQuery(function( $ ) { ...@@ -41,7 +42,7 @@ jQuery(function( $ ) {
var list = this.$todoList; var list = this.$todoList;
this.$newTodo.on( 'keyup', this.create ); this.$newTodo.on( 'keyup', this.create );
this.$toggleAll.on( 'change', this.toggleAll ); this.$toggleAll.on( 'change', this.toggleAll );
this.$clearBtn.on( 'click', this.destroyCompleted ); this.$footer.on( 'click', '#clear-completed', this.destroyCompleted );
list.on( 'change', '.toggle', this.toggle ); list.on( 'change', '.toggle', this.toggle );
list.on( 'dblclick', '.view', this.edit ); list.on( 'dblclick', '.view', this.edit );
list.on( 'keypress', '.edit', this.blurOnEnter ); list.on( 'keypress', '.edit', this.blurOnEnter );
...@@ -49,7 +50,7 @@ jQuery(function( $ ) { ...@@ -49,7 +50,7 @@ jQuery(function( $ ) {
list.on( 'click', '.destroy', this.destroy ); list.on( 'click', '.destroy', this.destroy );
}, },
render: function() { render: function() {
this.$todoList.html( this.template( this.todos ) ); this.$todoList.html( this.todoTemplate( this.todos ) );
this.$main.toggle( !!this.todos.length ); this.$main.toggle( !!this.todos.length );
this.$toggleAll.prop( 'checked', !this.activeTodoCount() ); this.$toggleAll.prop( 'checked', !this.activeTodoCount() );
this.renderFooter(); this.renderFooter();
...@@ -57,16 +58,15 @@ jQuery(function( $ ) { ...@@ -57,16 +58,15 @@ jQuery(function( $ ) {
}, },
renderFooter: function() { renderFooter: function() {
var todoCount = this.todos.length, var todoCount = this.todos.length,
activeTodos = this.activeTodoCount(), activeTodoCount = this.activeTodoCount(),
completedTodos = todoCount - activeTodos, footer = {
countTitle = '<strong>' + activeTodos + '</strong> ' + Utils.pluralize( activeTodos, 'item' ) + ' left', activeTodoCount: activeTodoCount,
clearTitle = 'Clear completed (' + completedTodos + ')'; activeTodoWord: Utils.pluralize( activeTodoCount, 'item' ),
// Only show the footer when there are at least one todo. completedTodos: todoCount - activeTodoCount
};
this.$footer.toggle( !!todoCount ); this.$footer.toggle( !!todoCount );
// Active todo count this.$footer.html( this.footerTemplate( footer ) );
this.$count.html( countTitle );
// Toggle clear button and update title
this.$clearBtn.text( clearTitle ).toggle( !!completedTodos );
}, },
toggleAll: function() { toggleAll: function() {
var isChecked = $( this ).prop('checked'); var isChecked = $( this ).prop('checked');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment