Commit b1850ce6 authored by Sindre Sorhus's avatar Sindre Sorhus

YUI app - code style improvements and HTML cleanup

parent 0839ee5f
......@@ -4,7 +4,10 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>YUI • TodoMVC</title>
<link rel="stylesheet" href="../../assets/base.css"/>
<link rel="stylesheet" href="../../assets/base.css">
<!--[if IE]>
<script src="../../assets/ie.js"></script>
<![endif]-->
</head>
<body>
<section id="todoapp">
......@@ -17,7 +20,7 @@
<label for="toggle-all">Mark all as complete</label>
<ul id="todo-list"></ul>
</section>
<footer id="footer"></footer>
<footer id="footer"></footer>
</section>
<footer id="info">
<p>Double-click to edit a todo</p>
......@@ -29,11 +32,11 @@
</footer>
<script type="text/x-handlebars-template" id="item-template">
<div class="view">
<input class="toggle" type="checkbox" {{#if completed}} checked="checked" {{/if}} />
<input class="toggle" type="checkbox" {{#if completed}}checked{{/if}}>
<label>{{title}}</label>
<button class="destroy"></button>
</div>
<input class="edit" type="text" value="{{title}}" />
<input class="edit" value="{{title}}">
</script>
<script type="text/x-handlebars-template" id="stats-template">
<span id="todo-count"><strong>{{remaining}}</strong> {{pluralize remaining "item"}} left</span>
......@@ -52,6 +55,7 @@
<button id="clear-completed">Clear completed ({{completed}})</button>
{{/if}}
</script>
<script src="../../assets/base.js"></script>
<script src="http://yui.yahooapis.com/3.6.0pr4/build/yui/yui-min.js"></script>
<script>
YUI({
......
YUI.add('todo-app', function (Y) {
"use strict";
'use strict';
// Dependencies from MVC namespace.
var TodoList = Y.TodoMVC.TodoList,
TodoView = Y.TodoMVC.TodoView,
TodoApp;
var TodoApp;
var TodoList = Y.TodoMVC.TodoList;
var TodoView = Y.TodoMVC.TodoView;
// -- Main Application --------------
TodoApp = Y.Base.create('todoApp', Y.App, [], {
......@@ -45,23 +45,21 @@ YUI.add('todo-app', function (Y) {
list.load();
// Keep our filters on refresh by immediately dispatching route.
this.once('ready', function (e) {
this.once('ready', function () {
if (this.hasRoute(this.getPath())) {
this.dispatch();
}
});
},
// Render our application with the statistics from our TodoList,
// and various other stylistic elements.
render: function () {
var todoList = this.get('todoList'),
completed = todoList.completed().size(),
remaining = todoList.remaining().size(),
container = this.get('container'),
main = this.get('main'),
footer = this.get('footer');
var todoList = this.get('todoList');
var completed = todoList.completed().size();
var remaining = todoList.remaining().size();
var main = this.get('main');
var footer = this.get('footer');
// If we have Todos in our TodoList, show them with statistics.
if (todoList.size()) {
......@@ -89,26 +87,25 @@ YUI.add('todo-app', function (Y) {
this.addViews();
},
// Add Todo views to the DOM simultaneously, triggered when
// the application initially loads, or we switch filters.
addViews: function () {
var fragment = Y.one(Y.config.doc.createDocumentFragment()),
todoList = this.get('todoList'),
models;
var models;
var fragment = Y.one(Y.config.doc.createDocumentFragment());
var todoList = this.get('todoList');
// An Array of models is passed through when the 'reset'
// event is triggered through syncing through load().
switch (this.get('filter')) {
case 'active':
models = todoList.remaining();
break;
case 'completed':
models = todoList.completed();
break;
default:
models = todoList;
break;
case 'active':
models = todoList.remaining();
break;
case 'completed':
models = todoList.completed();
break;
default:
models = todoList;
break;
}
// Iterate through the (filtered) ModelList.
......@@ -123,10 +120,10 @@ YUI.add('todo-app', function (Y) {
// Create and save a new Todo from the inputted value when the
// Enter key is pressed down.
enterCreate: function (e) {
var ENTER_KEY = 13,
todoList = this.get('todoList'),
inputNode = this.get('inputNode'),
value = Y.Escape.html(Y.Lang.trim(inputNode.get('value')));
var ENTER_KEY = 13;
var todoList = this.get('todoList');
var inputNode = this.get('inputNode');
var value = Y.Escape.html(Y.Lang.trim(inputNode.get('value')));
if (e.keyCode !== ENTER_KEY || !value) {
return;
......@@ -141,9 +138,9 @@ YUI.add('todo-app', function (Y) {
// Clear all completed Todos from the TodoList. This removes the models
// from the list, as well as deletes them from localStorage.
clearCompleted: function (e) {
var todoList = this.get('todoList'),
completed = todoList.completed();
clearCompleted: function () {
var todoList = this.get('todoList');
var completed = todoList.completed();
todoList.remove(completed);
......@@ -155,9 +152,9 @@ YUI.add('todo-app', function (Y) {
// Complete all non-complete Todos, or reset them all if they are
// all already complete.
completeAll: function () {
var todoList = this.get('todoList'),
allCheckbox = this.get('allCheckbox'),
completed = allCheckbox.get('checked');
var todoList = this.get('todoList');
var allCheckbox = this.get('allCheckbox');
var completed = allCheckbox.get('checked');
Y.Array.each(todoList.toArray(), function (todo) {
todo.save({completed: completed});
......@@ -216,7 +213,10 @@ YUI.add('todo-app', function (Y) {
// The callback takes a request object, Express-style.
routes: {
value: [
{path: '/:filter', callback: 'handleFilter'}
{
path: '/:filter',
callback: 'handleFilter'
}
]
}
}
......
YUI.add('todo', function (Y) {
"use strict";
'use strict';
// -- Todo Model -------------
var Todo = Y.Base.create('todo', Y.Model, [Y.ModelSync.Local], {
// Set up the root localStorage key we save our Model data in.
......@@ -15,11 +16,10 @@ YUI.add('todo', function (Y) {
this.destroy({remove: true});
}
}, {
// Default attributes.
ATTRS: {
title: {
value: 'empty todo ...'
value: ''
},
completed: {
value: false
......
YUI.add('todo-list', function (Y) {
"use strict";
'use strict';
// Dependencies from Y.MVC.
var Todo = Y.TodoMVC.Todo,
TodoList;
var TodoList;
var Todo = Y.TodoMVC.Todo;
// -- TodoList Model list -----
TodoList = Y.Base.create('todoList', Y.ModelList, [Y.ModelSync.Local], {
// The related Model for our Model List.
model: Todo,
......@@ -16,18 +15,17 @@ YUI.add('todo-list', function (Y) {
// Return a ModelList of our completed Models.
completed: function () {
return this.filter({ asList: true }, function (todo) {
return this.filter({asList: true}, function (todo) {
return todo.get('completed');
});
},
// Return an ModelList of our un-completed Models.
remaining: function () {
return this.filter({ asList: true }, function (todo) {
return this.filter({asList: true}, function (todo) {
return !todo.get('completed');
});
}
});
// Set this Model List under our custom Y.MVC namespace.
......
YUI.add('todo-view', function (Y) {
"use strict";
'use strict';
// -- Todo View -------------------
var TodoView = Y.Base.create('todoView', Y.View, [], {
......@@ -32,15 +32,14 @@ YUI.add('todo-view', function (Y) {
// is updated or destroyed.
initializer: function () {
var model = this.get('model');
model.after('change', this.render, this);
},
// Render this view in our <li> container, and fill it with the
// data in our Model.
render: function () {
var container = this.get('container'),
model = this.get('model');
var container = this.get('container');
var model = this.get('model');
container.setHTML(this.template(model.toJSON()));
container.toggleClass('completed', model.get('completed'));
......@@ -63,9 +62,9 @@ YUI.add('todo-view', function (Y) {
// Get the value from our input field while hiding it, and
// save it to our Todo when focus is lost from the field.
close: function (e) {
var value = this.get('inputNode').get('value'),
editedValue = Y.Escape.html(Y.Lang.trim(value));
close: function () {
var value = this.get('inputNode').get('value');
var editedValue = Y.Escape.html(Y.Lang.trim(value));
this.get('container').removeClass('editing');
......@@ -85,7 +84,7 @@ YUI.add('todo-view', function (Y) {
},
// Destroy the model when the delete button is clicked.
clear: function (e) {
clear: function () {
this.get('model').clear();
}
});
......
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