Commit d87b2b21 authored by Sindre Sorhus's avatar Sindre Sorhus

CanJS improvements

- Single quotes
- Tabs
- Code style
- Remove all except jQuery one from site
- etc
parent 04a93033
...@@ -157,6 +157,9 @@ ...@@ -157,6 +157,9 @@
<div class="span12"> <div class="span12">
<h2>Labs</h2> <h2>Labs</h2>
<ul class="applist"> <ul class="applist">
<li class="routing">
<a href="labs/architecture-examples/canjs/" data-source="http://canjs.us" data-content="CanJS with jQuery (includes a jQuery UI widget binding example). CanJS is a client-side, JavaScript framework that makes building rich web applications easy. It provides can.Model (for connecting to RESTful JSON interfaces), can.View (for template loading and caching), can.Observe (for key-value binding), can.EJS (live binding templates), can.Control (declarative event bindings) and can.route (routing support).">CanJS</a>
</li>
<li> <li>
<a href="labs/architecture-examples/maria/src/" data-source="https://github.com/petermichaux/maria" data-content="An MVC framework for JavaScript applications. The real MVC. The Smalltalk MVC. The Gang of Four MVC. The three core design patterns of MVC (observer, composite, and strategy) are embedded in Maria's Model, View, and Controller objects. Other patterns traditionally included in MVC implementations (e.g. factory method and template) make appearances too.">Maria</a> <a href="labs/architecture-examples/maria/src/" data-source="https://github.com/petermichaux/maria" data-content="An MVC framework for JavaScript applications. The real MVC. The Smalltalk MVC. The Gang of Four MVC. The three core design patterns of MVC (observer, composite, and strategy) are embedded in Maria's Model, View, and Controller objects. Other patterns traditionally included in MVC implementations (e.g. factory method and template) make appearances too.">Maria</a>
</li> </li>
...@@ -241,27 +244,6 @@ ...@@ -241,27 +244,6 @@
<li class="routing"> <li class="routing">
<a href="labs/architecture-examples/thorax" data-source="https://github.com/walmartlabs/thorax" data-content="Thorax is a Backbone superset that integrates deeply with Handlebars to provide easy model and collection binding.">Thorax</a> <a href="labs/architecture-examples/thorax" data-source="https://github.com/walmartlabs/thorax" data-content="Thorax is a Backbone superset that integrates deeply with Handlebars to provide easy model and collection binding.">Thorax</a>
</li> </li>
<li>
<a href="labs/architecture-examples/canjs/dojo/" data-source="http://canjs.us" data-content="CanJS with Dojo. CanJS is a client-side, JavaScript framework that makes building rich web applications easy. It provides can.Model (for connecting to RESTful JSON interfaces), can.View (for template loading and caching), can.Observe (for key-value binding), can.EJS (live binding templates), can.Control (declarative event bindings) and can.route (routing support).">CanJS (Dojo)</a>
</li>
<li>
<a href="labs/architecture-examples/canjs/dojo-widget/" data-source="http://canjs.us" data-content="CanJS with Dojo (includes a Dojo widget binding example). CanJS is a client-side, JavaScript framework that makes building rich web applications easy. It provides can.Model (for connecting to RESTful JSON interfaces), can.View (for template loading and caching), can.Observe (for key-value binding), can.EJS (live binding templates), can.Control (declarative event bindings) and can.route (routing support).">CanJS (Dojo Widget)</a>
</li>
<li>
<a href="labs/architecture-examples/canjs/jquery/" data-source="http://canjs.us" data-content="CanJS with jQuery. CanJS is a client-side, JavaScript framework that makes building rich web applications easy. It provides can.Model (for connecting to RESTful JSON interfaces), can.View (for template loading and caching), can.Observe (for key-value binding), can.EJS (live binding templates), can.Control (declarative event bindings) and can.route (routing support).">CanJS (jQuery)</a>
</li>
<li>
<a href="labs/architecture-examples/canjs/jquery-widget/" data-source="http://canjs.us" data-content="CanJS with jQuery (includes a jQuery UI widget binding example). CanJS is a client-side, JavaScript framework that makes building rich web applications easy. It provides can.Model (for connecting to RESTful JSON interfaces), can.View (for template loading and caching), can.Observe (for key-value binding), can.EJS (live binding templates), can.Control (declarative event bindings) and can.route (routing support).">CanJS (jQuery Widget)</a>
</li>
<li>
<a href="labs/architecture-examples/canjs/mootools/" data-source="http://canjs.us" data-content="CanJS with Mootools. CanJS is a client-side, JavaScript framework that makes building rich web applications easy. It provides can.Model (for connecting to RESTful JSON interfaces), can.View (for template loading and caching), can.Observe (for key-value binding), can.EJS (live binding templates), can.Control (declarative event bindings) and can.route (routing support).">CanJS (MooTools)</a>
</li>
<li>
<a href="labs/architecture-examples/canjs/yui/" data-source="http://canjs.us" data-content="CanJS with YUI. CanJS is a client-side, JavaScript framework that makes building rich web applications easy. It provides can.Model (for connecting to RESTful JSON interfaces), can.View (for template loading and caching), can.Observe (for key-value binding), can.EJS (live binding templates), can.Control (declarative event bindings) and can.route (routing support).">CanJS (YUI)</a>
</li>
<li>
<a href="labs/architecture-examples/canjs/yui-widget/" data-source="http://canjs.us" data-content="CanJS with YUI (includes a YUI widget binding example). CanJS is a client-side, JavaScript framework that makes building rich web applications easy. It provides can.Model (for connecting to RESTful JSON interfaces), can.View (for template loading and caching), can.Observe (for key-value binding), can.EJS (live binding templates), can.Control (declarative event bindings) and can.route (routing support).">CanJS (YUI Widget)</a>
</li>
<li class="routing"> <li class="routing">
<a href="labs/architecture-examples/dart/web/" data-source="http://dartlang.org" data-content="Dart firstly targets the development of modern and large scale browser-side web apps. It's an object oriented language with a C-style syntax. It has two run modes : it can be compiled to JS, and will later run in native VM in compliant browsers (just in a dedicated Chromium provided with Dart SDK for the moment).">Dart</a> <a href="labs/architecture-examples/dart/web/" data-source="http://dartlang.org" data-content="Dart firstly targets the development of modern and large scale browser-side web apps. It's an object oriented language with a C-style syntax. It has two run modes : it can be compiled to JS, and will later run in native VM in compliant browsers (just in a dedicated Chromium provided with Dart SDK for the moment).">Dart</a>
</li> </li>
......
.hidden {
display: none;
}
\ No newline at end of file
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<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>CanJS • TodoMVC</title> <title>CanJS • 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]-->
</head> </head>
<body> <body>
<section id="todoapp"> <section id="todoapp"></section>
</section> <div id="info">
<div id="info"> <p>Double-click to edit a todo</p>
<p>Double-click to edit a todo</p> <p>Written by <a href="http://bitovi.com">Bitovi</a></p>
<p>Written by <a href="http://bitovi.com">Bitovi</a></p> <p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p> </div>
</div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script src="../../../assets/base.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script> <script src="js/lib/can.jquery-1.0.7.min.js"></script>
<script src="../../../assets/base.js"></script> <script src="js/lib/can-localstorage.min.js"></script>
<script src="js/models/todo.js"></script>
<script src="js/lib/can.jquery-1.0.7.min.js"></script> <script src="js/todos/todos.js"></script>
<script src="js/lib/can-localstorage.min.js"></script> <script src="js/app.js"></script>
<script src="js/models/todo.js"></script>
<script src="js/todos/todos.js"></script>
<script src="js/app.js"></script>
</body> </body>
</html> </html>
(function () { (function() {
$(function () { $(function() {
can.route(':filter'); can.route(':filter');
// Initialize the app // Initialize the app
Models.Todo.findAll({}, function (todos) { Models.Todo.findAll({}, function(todos) {
new Todos('#todoapp', { new Todos('#todoapp', {
todos : todos, todos: todos,
state : can.route state: can.route
}); });
}); });
}) });
})(); })();
/*global can */
(function (namespace, undefined) { (function (namespace, undefined) {
'use strict';
// Basic Todo entry model // Basic Todo entry model
// { text: 'todo', complete: false } // { text: 'todo', complete: false }
var Todo = can.Model.LocalStorage({ var Todo = can.Model.LocalStorage({
storageName : 'todos-canjs' storageName: 'todos-canjs'
}, { }, {
// Returns if this instance matches a given filter // Returns if this instance matches a given filter
// (currently `active` and `complete`) // (currently `active` and `complete`)
matches : function (filter) { matches: function(filter) {
return !filter || (filter === 'active' && !this.attr('complete')) return !filter || (filter === 'active' && !this.attr('complete'))
|| (filter === 'completed' && this.attr('complete')); || (filter === 'completed' && this.attr('complete'));
} }
...@@ -14,24 +17,25 @@ ...@@ -14,24 +17,25 @@
// List for Todos // List for Todos
Todo.List = can.Model.List({ Todo.List = can.Model.List({
completed : function () { completed: function() {
var completed = 0; var completed = 0;
this.each(function (todo) {
this.each(function(todo) {
completed += todo.attr('complete') ? 1 : 0; completed += todo.attr('complete') ? 1 : 0;
}); });
return completed; return completed;
}, },
remaining : function () { remaining: function() {
return this.attr('length') - this.completed(); return this.attr('length') - this.completed();
}, },
allComplete : function () { allComplete: function() {
return this.attr('length') === this.completed(); return this.attr('length') === this.completed();
} }
}); });
namespace.Models = namespace.Models || {}; namespace.Models = namespace.Models || {};
namespace.Models.Todo = Todo; namespace.Models.Todo = Todo;
})(this); })(this);
\ No newline at end of file
/*global can, Models, $ */
(function (namespace, undefined) { (function (namespace, undefined) {
'use strict';
var ENTER_KEY = 13;
var Todos = can.Control({ var Todos = can.Control({
// Initialize the Todos list // Initialize the Todos list
init : function () { init: function() {
// Render the Todos // Render the Todos
this.element.append(can.view('todos.ejs', this.options)); this.element.append(can.view('todos.ejs', this.options));
...@@ -11,9 +15,9 @@ ...@@ -11,9 +15,9 @@
}, },
// Listen for when a new Todo has been entered // Listen for when a new Todo has been entered
'#new-todo keyup' : function (el, ev) { '#new-todo keyup': function(el, e) {
var value = can.trim(el.val()); var value = can.trim(el.val());
if (ev.keyCode == 13 && value !== '') { if (e.keyCode === ENTER_KEY && value !== '') {
new Models.Todo({ new Models.Todo({
text : value, text : value,
complete : false complete : false
...@@ -24,25 +28,25 @@ ...@@ -24,25 +28,25 @@
}, },
// Handle a newly created Todo // Handle a newly created Todo
'{Models.Todo} created' : function (list, ev, item) { '{Models.Todo} created': function(list, e, item) {
this.options.todos.push(item); this.options.todos.push(item);
// Reset the filter so that you always see your new todo // Reset the filter so that you always see your new todo
this.options.state.removeAttr('filter') this.options.state.removeAttr('filter');
}, },
// Listen for editing a Todo // Listen for editing a Todo
'.todo dblclick' : function (el, ev) { '.todo dblclick': function(el) {
el.data('todo').attr('editing', true).save(function () { el.data('todo').attr('editing', true).save(function() {
el.children('.edit').focus(); el.children('.edit').focus();
}); });
}, },
// Update a todo // Update a todo
updateTodo : function (el) { updateTodo: function(el) {
var value = can.trim(el.val()), var value = can.trim(el.val()),
todo = el.closest('.todo').data('todo'); todo = el.closest('.todo').data('todo');
if (value == '') { if (value === '') {
todo.destroy(); todo.destroy();
} else { } else {
todo.attr({ todo.attr({
...@@ -53,37 +57,37 @@ ...@@ -53,37 +57,37 @@
}, },
// Listen for an edited Todo // Listen for an edited Todo
'.todo .edit keyup' : function (el, ev) { '.todo .edit keyup': function(el, e) {
if (ev.keyCode == 13) { if (e.keyCode === ENTER_KEY) {
this.updateTodo(el); this.updateTodo(el);
} }
}, },
'.todo .edit focusout' : function (el, ev) { '.todo .edit focusout': function(el) {
this.updateTodo(el); this.updateTodo(el);
}, },
// Listen for the toggled completion of a Todo // Listen for the toggled completion of a Todo
'.todo .toggle click' : function (el, ev) { '.todo .toggle click': function(el) {
el.closest('.todo').data('todo') el.closest('.todo').data('todo')
.attr('complete', el.is(':checked')) .attr('complete', el.is(':checked'))
.save(); .save();
}, },
// Listen for a removed Todo // Listen for a removed Todo
'.todo .destroy click' : function (el) { '.todo .destroy click': function(el) {
el.closest('.todo').data('todo').destroy(); el.closest('.todo').data('todo').destroy();
}, },
// Listen for toggle all completed Todos // Listen for toggle all completed Todos
'#toggle-all click' : function (el, ev) { '#toggle-all click': function (el) {
var toggle = el.prop('checked'); var toggle = el.prop('checked');
can.each(this.options.todos, function (todo) { can.each(this.options.todos, function(todo) {
todo.attr('complete', toggle).save(); todo.attr('complete', toggle).save();
}); });
}, },
// Listen for removing all completed Todos // Listen for removing all completed Todos
'#clear-completed click' : function () { '#clear-completed click': function() {
for (var i = this.options.todos.length - 1, todo; i > -1 && (todo = this.options.todos[i]); i--) { for (var i = this.options.todos.length - 1, todo; i > -1 && (todo = this.options.todos[i]); i--) {
if (todo.attr('complete')) { if (todo.attr('complete')) {
todo.destroy(); todo.destroy();
...@@ -95,4 +99,4 @@ ...@@ -95,4 +99,4 @@
}); });
namespace.Todos = Todos; namespace.Todos = Todos;
})(this); })(this);
\ No newline at end of file
<header id="header"> <header id="header">
<h1>todos</h1> <h1>todos</h1>
<input id="new-todo" placeholder="What needs to be done?" autofocus=""> <input id="new-todo" placeholder="What needs to be done?" autofocus>
</header> </header>
<section id="main" class="<%= todos.attr("length") > 0 ? 'show' : '' %>">
<section id="main" class="<%= todos.attr("length") > 0 ? "show" : "" %>"> <input id="toggle-all" type="checkbox" <%= todos.allComplete() ? 'checked' : '' %>>
<input id="toggle-all" type="checkbox" <%= todos.allComplete() ? "checked" : "" %>>
<label for="toggle-all">Mark all as complete</label> <label for="toggle-all">Mark all as complete</label>
<ul id="todo-list"> <ul id="todo-list">
<% todos.each(function( todo ) { %> <% todos.each(function( todo ) { %>
<li class="todo <li class="todo
<%= todo.matches(state.attr('filter')) ? "" : "hidden" %> <%= todo.matches(state.attr('filter')) ? '' : 'hidden' %>
<%= todo.attr("complete") ? "completed" : "" %> <%= todo.attr('complete') ? 'completed' : '' %>
<%= todo.attr("editing") ? "editing" : "" %>" <%= todo.attr('editing') ? 'editing' : '' %>"
<%= (el)-> el.data('todo', todo) %>> <%= (el)-> el.data('todo', todo) %>>
<div class="view"> <div class="view">
<input class="toggle" type="checkbox" <%= todo.attr("complete") ? "checked" : "" %>> <input class="toggle" type="checkbox" <%= todo.attr('complete') ? 'checked' : '' %>>
<label><%= todo.attr("text") %></label> <label><%= todo.attr('text') %></label>
<button class="destroy"></button> <button class="destroy"></button>
</div> </div>
<input class="edit" type="text" value="<%= todo.attr("text") %>"> <input class="edit" value="<%= todo.attr('text') %>">
</li> </li>
<% }) %> <% }) %>
</ul> </ul>
</section> </section>
<footer id="footer" class="<%= todos.attr('length') === 0 ? 'hidden' : '' %>"> <footer id="footer" class="<%= todos.attr('length') === 0 ? 'hidden' : '' %>">
<span id="todo-count"> <span id="todo-count">
<strong><%= todos.remaining() %></strong> <strong><%= todos.remaining() %></strong>
item<%= todos.remaining() == 1 ? "" : "s" %> left item<%= todos.remaining() == 1 ? '' : 's' %> left
</span> </span>
<ul id="filters"> <ul id="filters">
<li> <li>
<a class="<%= !state.attr('filter') ? 'selected' : '' %>" href="#!">All</a> <a class="<%= !state.attr('filter') ? 'selected' : '' %>" href="#!">All</a>
</li> </li>
<li> <li>
<a class="<%= state.attr('filter') == 'active' ? 'selected' : '' %>" href="#!active"> <a class="<%= state.attr('filter') === 'active' ? 'selected' : '' %>" href="#!active">
Active Active
</a> </a>
</li> </li>
<li> <li>
<a class="<%= state.attr('filter') == 'completed' ? 'selected' : '' %>" href="#!completed"> <a class="<%= state.attr('filter') === 'completed' ? 'selected' : '' %>" href="#!completed">
Completed Completed
</a> </a>
</li> </li>
</ul> </ul>
<button id="clear-completed" class="<%= todos.completed() === 0 ? 'hidden' : '' %>"> <button id="clear-completed" class="<%= todos.completed() === 0 ? 'hidden' : '' %>">
Clear <%= todos.completed() %> Clear <%= todos.completed() %>
completed item<%= todos.completed() == 1 ? "" : "s" %> completed item<%= todos.completed() === 1 ? '' : 's' %>
</button> </button>
</footer> </footer>
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