Commit a543e298 authored by Sindre Sorhus's avatar Sindre Sorhus

Flight - code style

parent 994d727a
/*global define */
'use strict';
define(
[
'./data/todos',
'./data/stats',
......@@ -25,11 +25,11 @@ define(
StatsData.attachTo(document);
TodosData.attachTo(document);
NewItemUI.attachTo('#new-todo');
MainSelectorUI.attachTo("#main");
MainSelectorUI.attachTo('#main');
StatsUI.attachTo('#footer');
ToggleAllUI.attachTo('#toggle-all');
TodoListUI.attachTo('#todo-list');
}
};
return {
initialize: initialize
......
/*global define */
'use strict';
define(
[
'flight/component',
'../store'
],
function (defineComponent, dataStore) {
return defineComponent(stats);
function stats() {
......@@ -16,7 +15,7 @@ define(
var todos = dataStore.all();
var all = todos.length;
var remaining = todos.reduce(function (memo, each) {
return memo += (each.completed) ? 0 : 1;
return memo += each.completed ? 0 : 1;
}, 0);
this.trigger('dataStatsCounted', {
......@@ -25,7 +24,7 @@ define(
completed: all - remaining,
filter: localStorage.getItem('filter') || ''
});
}
};
this.after('initialize', function () {
this.on(document, 'dataTodosLoaded', this.recount);
......
/*global define */
'use strict';
define(
[
'flight/component',
'../store'
],
function (defineComponent, dataStore) {
return defineComponent(todos);
function todos() {
var filter;
this.add = function (e, data) {
......@@ -22,13 +20,13 @@ define(
});
this.trigger('dataTodoAdded', { todo: todo, filter: filter });
}
};
this.remove = function (e, data) {
var todo = dataStore.destroy(data.id);
this.trigger('dataTodoRemoved', todo);
}
};
this.load = function (e, data) {
var todos;
......@@ -36,11 +34,11 @@ define(
filter = localStorage.getItem('filter');
todos = this.find();
this.trigger('dataTodosLoaded', { todos: todos });
}
};
this.update = function (e, data) {
dataStore.save(data);
}
};
this.toggleCompleted = function (e, data) {
var eventType;
......@@ -49,15 +47,15 @@ define(
todo.completed = !todo.completed;
dataStore.save(todo);
eventType = (filter) ? 'dataTodoRemoved' : 'dataTodoToggled';
eventType = filter ? 'dataTodoRemoved' : 'dataTodoToggled';
this.trigger(eventType, todo);
}
};
this.toggleAllCompleted = function (e, data) {
dataStore.updateAll({ completed: data.completed });
this.trigger('dataTodoToggledAll', { todos: this.find(filter) });
}
};
this.filter = function (e, data) {
var todos;
......@@ -67,7 +65,7 @@ define(
todos = this.find();
this.trigger('dataTodosFiltered', { todos: todos });
}
};
this.find = function () {
var todos;
......@@ -82,7 +80,7 @@ define(
}
return todos;
}
};
this.clearCompleted = function () {
var todos;
......@@ -90,7 +88,7 @@ define(
dataStore.destroyAll({ completed: true });
todos = dataStore.all();
this.trigger('dataClearedCompleted', { todos: todos });
}
};
this.after('initialize', function () {
this.on(document, 'uiAddRequested', this.add);
......
/*global define */
'use strict';
define(
[
'flight/component'
],
function (defineComponent) {
return defineComponent(mainSelector);
function mainSelector() {
this.toggle = function (e, data) {
var toggle = data.all > 0;
this.$node.toggle(toggle);
}
};
this.after('initialize', function () {
this.$node.hide();
......
/*global define */
'use strict';
define(
[
'flight/component'
],
function (defineComponent) {
return defineComponent(newItem);
function newItem() {
var ENTER_KEY = 13;
this.createOnEnter = function (e) {
......@@ -25,7 +23,7 @@ define(
});
this.$node.val('');
}
};
this.after('initialize', function () {
this.on('keydown', this.createOnEnter);
......
/*global define */
'use strict';
define(
[
'flight/component',
'./with_filters',
......@@ -10,7 +10,6 @@ define(
],
function (defineComponent, withFilters, statsTmpl, utils) {
return defineComponent(stats, withFilters);
function stats() {
......@@ -26,11 +25,11 @@ define(
this.$node.html(template(data));
this.$node.toggle(toggle);
this.markSelected(data.filter);
}
};
this.clearCompleted = function (e, data) {
this.trigger('uiClearRequested');
}
};
this.after('initialize', function () {
this.$node.hide();
......
/*global define $ */
'use strict';
define(
[
'flight/component',
'text!app/templates/todo.html',
......@@ -9,11 +9,9 @@ define(
],
function (defineComponent, todoTmpl, utils) {
return defineComponent(todoList);
function todoList() {
var ENTER_KEY = 13;
var template = utils.tmpl(todoTmpl);
......@@ -29,23 +27,22 @@ define(
data.todos.forEach(function (each) {
this.render(e, { todo: each });
}, this);
}
};
this.render = function (e, data) {
if (e.type == 'dataTodoAdded'
&& data.filter == 'completed') {
if (e.type === 'dataTodoAdded' && data.filter === 'completed') {
return;
}
this.$node.append(template(data.todo));
}
};
this.edit = function (e, data) {
var $todoEl = $(data.el).parents('li');
$todoEl.addClass('editing');
this.select('editSelector').focus();
}
};
this.requestUpdate = function (e, data) {
var $inputEl = $(e.currentTarget);
......@@ -65,30 +62,30 @@ define(
} else {
this.trigger('uiRemoveRequested', { id: id });
}
},
};
this.requestUpdateOnEnter = function (e, data) {
if (e.which === ENTER_KEY) {
this.requestUpdate(e, data);
}
},
};
this.requestRemove = function (e, data) {
var id = $(data.el).attr('id').split('_')[1];
this.trigger('uiRemoveRequested', { id: id });
}
};
this.remove = function (e, data) {
var $todoEl = this.$node.find('#' + data.id);
$todoEl.remove();
}
};
this.toggle = function (e, data) {
var $todoEl = $(data.el).parents('li');
$todoEl.toggleClass('completed');
this.trigger('uiToggleRequested', { id: $todoEl.attr('id') });
}
};
this.after('initialize', function () {
this.on(document, 'dataTodoAdded', this.render);
......
/*global define */
'use strict';
define(
[
'flight/component'
],
function (defineComponent) {
return defineComponent(toggleAll);
function toggleAll() {
......@@ -15,11 +14,11 @@ define(
this.trigger('uiToggleAllRequested', {
completed: this.$node.is(':checked')
});
}
};
this.toggleCheckbox = function (e, data) {
this.$node[0].checked = !data.remaining;
}
};
this.after('initialize', function () {
this.on('click', this.toggleAllComplete);
......
/*global define $ */
'use strict';
define(
function() {
return withFilters;
function withFilters() {
function () {
return function withFilters() {
this.defaultAttrs({
filterSelector: '#filters a'
});
......@@ -17,15 +14,15 @@ define(
this.select('filterSelector').removeClass('selected');
$(data.el).addClass('selected');
this.trigger('uiFilterRequested', { filter: filter });
}
};
this.markSelected = function (filter) {
this.$node.find('[href="#/' + filter + '"]').addClass('selected');
}
};
this.after('initialize', function() {
this.after('initialize', function () {
this.on('click', { filterSelector: this.chooseFilter });
});
}
};
}
);
/*global define */
'use strict';
// tmpl function scooped from underscore.
......@@ -13,6 +14,7 @@ define(function () {
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
/*jshint quotmark:false */
"'": '&#x27;',
'/': '&#x2F;'
}
......@@ -28,23 +30,27 @@ define(function () {
};
// Functions for escaping and unescaping strings to/from HTML interpolation.
['escape', 'unescape'].forEach(function(method) {
_[method] = function(string) {
if (string == null) return '';
return ('' + string).replace(entityRegexes[method], function(match) {
['escape', 'unescape'].forEach(function (method) {
_[method] = function (string) {
if (string === null || string === undefined) {
return '';
}
return ('' + string).replace(entityRegexes[method], function (match) {
return entityMap[method][match];
});
};
});
var settings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
escape: /<%-([\s\S]+?)%>/g
};
var noMatch = /(.)^/;
var escapes = {
/*jshint quotmark:false */
"'": "'",
'\\': '\\',
'\r': 'r',
......@@ -59,7 +65,7 @@ define(function () {
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
var template = function(text, data) {
var template = function (text, data) {
var render;
// Combine delimiters into one regular expression via alternation.
......@@ -72,9 +78,11 @@ define(function () {
// Compile the template source, escaping string literals appropriately.
var index = 0;
var source = "__p+='";
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
text.replace(matcher, function (match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset)
.replace(escaper, function(match) { return '\\' + escapes[match]; });
.replace(escaper, function (match) {
return '\\' + escapes[match];
});
if (escape) {
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
......@@ -91,7 +99,9 @@ define(function () {
source += "';\n";
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
if (!settings.variable) {
source = 'with(obj||{}){\n' + source + '}\n';
}
source = "var __t,__p='',__j=Array.prototype.join," +
"print=function(){__p+=__j.call(arguments,'');};\n" +
......@@ -99,13 +109,16 @@ define(function () {
try {
render = new Function(settings.variable || 'obj', '_', source);
} catch (e) {
e.source = source;
throw e;
} catch (err) {
err.source = source;
throw err;
}
if (data) {
return render(data, _);
}
if (data) return render(data, _);
var template = function(data) {
var template = function (data) {
return render.call(this, data, _);
};
......
......@@ -6,7 +6,7 @@
<title>Flight • Todo</title>
<link rel="stylesheet" href="../../assets/base.css">
</head>
<body>
<body>
<section id="todoapp">
<header id="header">
<h1>todos</h1>
......@@ -24,6 +24,7 @@
<p>Created by <a href="http://github.com/mkuklis">Michal Kuklis</a></p>
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
</footer>
<script src="../../assets/base.js"></script>
<script data-main="app/js/main" src="components/requirejs/requirejs.js"></script>
</body>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Template • TodoMVC</title>
......@@ -10,8 +10,8 @@
<!--[if IE]>
<script src="../assets/ie.js"></script>
<![endif]-->
</head>
<body>
</head>
<body>
<section id="todoapp">
<header id="header">
<h1>todos</h1>
......@@ -72,5 +72,5 @@
<!-- Scripts here. Don't remove this ↓ -->
<script src="../assets/base.js"></script>
<script src="js/app.js"></script>
</body>
</body>
</html>
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