todomvc
Version:
> Helping you select an MV\* framework
208 lines (161 loc) • 5.32 kB
JavaScript
// Generated by CoffeeScript 1.7.1
(function() {
var Alfred,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Alfred = (function(_super) {
__extends(Alfred, _super);
function Alfred() {
return Alfred.__super__.constructor.apply(this, arguments);
}
Alfred.root('todos#index', {
as: "all"
});
Alfred.route('/completed', 'todos#completed');
Alfred.route('/active', 'todos#active');
return Alfred;
})(Batman.App);
Alfred.TodosController = (function(_super) {
__extends(TodosController, _super);
function TodosController() {
TodosController.__super__.constructor.apply(this, arguments);
this.set('newTodo', new Alfred.Todo({
completed: false
}));
}
TodosController.prototype.routingKey = 'todos';
TodosController.prototype.index = function() {
return this.set('currentTodoSet', 'default');
};
TodosController.prototype.completed = function() {
this.set('currentTodoSet', 'completed');
return this.render({
source: 'todos/index'
});
};
TodosController.prototype.active = function() {
this.set('currentTodoSet', 'active');
return this.render({
source: 'todos/index'
});
};
return TodosController;
})(Batman.Controller);
Alfred.TodosIndexView = (function(_super) {
__extends(TodosIndexView, _super);
function TodosIndexView() {
return TodosIndexView.__super__.constructor.apply(this, arguments);
}
TodosIndexView.prototype.createTodo = function(node, event, context) {
var newTodo, title;
event.preventDefault();
title = node[0].value.trim();
if (title) {
newTodo = new Alfred.Todo({
title: title,
completed: false
});
return newTodo.save(function(err) {
if (!err) {
return node[0].value = null;
}
});
}
};
TodosIndexView.prototype.destroyTodo = function(node, event, context) {
var todo;
todo = context.get('todo');
return todo.destroy(function(err) {
if (err) {
throw err;
}
});
};
TodosIndexView.prototype.toggleAll = function(node, context) {
return Alfred.Todo.get('all').forEach(function(todo) {
return todo.set('completed', !!node.checked);
});
};
TodosIndexView.prototype.clearCompleted = function() {
return Alfred.Todo.get('completed').forEach(function(todo) {
return todo.destroy(function(err) {
if (err) {
throw err;
}
});
});
};
TodosIndexView.prototype.toggleEditing = function(node, event, context) {
var editing, input, todo;
todo = context.get('todo');
editing = todo.set('editing', !todo.get('editing'));
if (editing) {
input = document.getElementById("todo-input-" + (todo.get('id')));
return input.focus();
}
};
TodosIndexView.prototype.disableEditingOnEnter = function(node, event, context) {
if (Batman.DOM.events.isEnter(event)) {
return node.blur();
}
};
return TodosIndexView;
})(Batman.View);
Alfred.TodosActiveView = (function(_super) {
__extends(TodosActiveView, _super);
function TodosActiveView() {
return TodosActiveView.__super__.constructor.apply(this, arguments);
}
return TodosActiveView;
})(Alfred.TodosIndexView);
Alfred.TodosCompletedView = (function(_super) {
__extends(TodosCompletedView, _super);
function TodosCompletedView() {
return TodosCompletedView.__super__.constructor.apply(this, arguments);
}
return TodosCompletedView;
})(Alfred.TodosIndexView);
Alfred.Todo = (function(_super) {
__extends(Todo, _super);
Todo.persist(Batman.LocalStorage);
Todo.storageKey = 'todos-batman';
function Todo() {
Todo.__super__.constructor.apply(this, arguments);
this.observe('completed', function(newValue, oldValue) {
return this.save();
});
this.observe('editing', function(newValue, oldValue) {
if (newValue === false) {
if (this.get('title').length > 0) {
this.set('title', this.get('title').trim());
return this.save();
} else {
return this.destroy();
}
}
});
this;
}
Todo.encode('title', 'completed');
Todo.validate('title', {
presence: true
});
Todo.classAccessor('default', function() {
return this.get('all').filter(function(todo) {
return true;
});
});
Todo.classAccessor('active', function() {
return this.get('all').filter(function(todo) {
return !todo.get('completed');
});
});
Todo.classAccessor('completed', function() {
return this.get('all').filter(function(todo) {
return todo.get('completed');
});
});
return Todo;
})(Batman.Model);
this.Alfred = Alfred;
}).call(this);