todomvc
Version:
> Helping you select an MV\* framework
121 lines (117 loc) • 3.54 kB
JavaScript
// Generated by CoffeeScript 1.7.1
(function() {
var ENTER_KEY;
ENTER_KEY = 13;
window.AppViewModel = function() {
var filter_fn, router;
this.collections = {
todos: new TodoCollection()
};
this.collections.todos.fetch();
this.list_filter_mode = ko.observable('');
filter_fn = ko.computed((function(_this) {
return function() {
switch (_this.list_filter_mode()) {
case 'active':
return function(model) {
return model.completed();
};
case 'completed':
return function(model) {
return !model.completed();
};
default:
return function() {
return false;
};
}
};
})(this));
this.todos = kb.collectionObservable(this.collections.todos, {
view_model: TodoViewModel,
filters: filter_fn
});
this.todos_changed = kb.triggeredObservable(this.collections.todos, 'change add remove');
this.tasks_exist = ko.computed((function(_this) {
return function() {
_this.todos_changed();
return !!_this.collections.todos.length;
};
})(this));
this.title = ko.observable('');
this.onAddTodo = (function(_this) {
return function(view_model, event) {
if (!$.trim(_this.title()) || (event.keyCode !== ENTER_KEY)) {
return true;
}
_this.collections.todos.create({
title: $.trim(_this.title())
});
return _this.title('');
};
})(this);
this.remaining_count = ko.computed((function(_this) {
return function() {
_this.todos_changed();
return _this.collections.todos.remainingCount();
};
})(this));
this.completed_count = ko.computed((function(_this) {
return function() {
_this.todos_changed();
return _this.collections.todos.completedCount();
};
})(this));
this.all_completed = ko.computed({
read: (function(_this) {
return function() {
return !_this.remaining_count();
};
})(this),
write: (function(_this) {
return function(completed) {
return _this.collections.todos.completeAll(completed);
};
})(this)
});
this.onDestroyCompleted = (function(_this) {
return function() {
return _this.collections.todos.destroyCompleted();
};
})(this);
this.loc = {
remaining_message: ko.computed((function(_this) {
return function() {
return "<strong>" + (_this.remaining_count()) + "</strong> " + (_this.remaining_count() === 1 ? 'item' : 'items') + " left";
};
})(this)),
clear_message: ko.computed((function(_this) {
return function() {
var count;
if ((count = _this.completed_count())) {
return "Clear completed (" + count + ")";
} else {
return '';
}
};
})(this))
};
router = new Backbone.Router;
router.route('', null, (function(_this) {
return function() {
return _this.list_filter_mode('');
};
})(this));
router.route('active', null, (function(_this) {
return function() {
return _this.list_filter_mode('active');
};
})(this));
router.route('completed', null, (function(_this) {
return function() {
return _this.list_filter_mode('completed');
};
})(this));
Backbone.history.start();
};
}).call(this);