Commit b1850ce6 authored by Sindre Sorhus's avatar Sindre Sorhus

YUI app - code style improvements and HTML cleanup

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