Commit 0eb61b8c authored by Duilio Protti's avatar Duilio Protti

Knockback - cancel editing on escape keypress

parent 9fc18e15
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.7.1
(function() { (function() {
ko.bindingHandlers.dblclick = { ko.bindingHandlers.dblclick = {
init: function(element, value_accessor) { init: function(element, value_accessor) {
return $(element).dblclick(ko.utils.unwrapObservable(value_accessor())); return $(element).dblclick(ko.utils.unwrapObservable(value_accessor()));
......
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.7.1
(function() { (function() {
var __hasProp = {}.hasOwnProperty, var __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; }; __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; };
window.Todo = (function(_super) { window.Todo = (function(_super) {
__extends(Todo, _super); __extends(Todo, _super);
function Todo() { function Todo() {
......
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.7.1
(function() { (function() {
var __hasProp = {}.hasOwnProperty, var __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; }; __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; };
window.TodoCollection = (function(_super) { window.TodoCollection = (function(_super) {
__extends(TodoCollection, _super); __extends(TodoCollection, _super);
function TodoCollection() { function TodoCollection() {
......
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.7.1
(function() { (function() {
var ENTER_KEY; var ENTER_KEY;
ENTER_KEY = 13; ENTER_KEY = 13;
window.AppViewModel = function() { window.AppViewModel = function() {
var filter_fn, router, var filter_fn, router;
_this = this;
this.collections = { this.collections = {
todos: new TodoCollection() todos: new TodoCollection()
}; };
this.collections.todos.fetch(); this.collections.todos.fetch();
this.list_filter_mode = ko.observable(''); this.list_filter_mode = ko.observable('');
filter_fn = ko.computed(function() { filter_fn = ko.computed((function(_this) {
switch (_this.list_filter_mode()) { return function() {
case 'active': switch (_this.list_filter_mode()) {
return function(model) { case 'active':
return model.completed(); return function(model) {
}; return model.completed();
case 'completed': };
return function(model) { case 'completed':
return !model.completed(); return function(model) {
}; return !model.completed();
default: };
return function() { default:
return false; return function() {
}; return false;
} };
}); }
};
})(this));
this.todos = kb.collectionObservable(this.collections.todos, { this.todos = kb.collectionObservable(this.collections.todos, {
view_model: TodoViewModel, view_model: TodoViewModel,
filters: filter_fn filters: filter_fn
}); });
this.todos_changed = kb.triggeredObservable(this.collections.todos, 'change add remove'); this.todos_changed = kb.triggeredObservable(this.collections.todos, 'change add remove');
this.tasks_exist = ko.computed(function() { this.tasks_exist = ko.computed((function(_this) {
_this.todos_changed(); return function() {
return !!_this.collections.todos.length; _this.todos_changed();
}); return !!_this.collections.todos.length;
};
})(this));
this.title = ko.observable(''); this.title = ko.observable('');
this.onAddTodo = function(view_model, event) { this.onAddTodo = (function(_this) {
if (!$.trim(_this.title()) || (event.keyCode !== ENTER_KEY)) { return function(view_model, event) {
return true; if (!$.trim(_this.title()) || (event.keyCode !== ENTER_KEY)) {
} return true;
_this.collections.todos.create({ }
title: $.trim(_this.title()) _this.collections.todos.create({
}); title: $.trim(_this.title())
return _this.title(''); });
}; return _this.title('');
this.remaining_count = ko.computed(function() { };
_this.todos_changed(); })(this);
return _this.collections.todos.remainingCount(); this.remaining_count = ko.computed((function(_this) {
}); return function() {
this.completed_count = ko.computed(function() { _this.todos_changed();
_this.todos_changed(); return _this.collections.todos.remainingCount();
return _this.collections.todos.completedCount(); };
}); })(this));
this.completed_count = ko.computed((function(_this) {
return function() {
_this.todos_changed();
return _this.collections.todos.completedCount();
};
})(this));
this.all_completed = ko.computed({ this.all_completed = ko.computed({
read: function() { read: (function(_this) {
return !_this.remaining_count(); return function() {
}, return !_this.remaining_count();
write: function(completed) { };
return _this.collections.todos.completeAll(completed); })(this),
} write: (function(_this) {
return function(completed) {
return _this.collections.todos.completeAll(completed);
};
})(this)
}); });
this.onDestroyCompleted = function() { this.onDestroyCompleted = (function(_this) {
return _this.collections.todos.destroyCompleted(); return function() {
}; return _this.collections.todos.destroyCompleted();
};
})(this);
this.loc = { this.loc = {
remaining_message: ko.computed(function() { remaining_message: ko.computed((function(_this) {
return "<strong>" + (_this.remaining_count()) + "</strong> " + (_this.remaining_count() === 1 ? 'item' : 'items') + " left"; return function() {
}), return "<strong>" + (_this.remaining_count()) + "</strong> " + (_this.remaining_count() === 1 ? 'item' : 'items') + " left";
clear_message: ko.computed(function() { };
var count; })(this)),
if ((count = _this.completed_count())) { clear_message: ko.computed((function(_this) {
return "Clear completed (" + count + ")"; return function() {
} else { var count;
return ''; if ((count = _this.completed_count())) {
} return "Clear completed (" + count + ")";
}) } else {
return '';
}
};
})(this))
}; };
router = new Backbone.Router; router = new Backbone.Router;
router.route('', null, function() { router.route('', null, (function(_this) {
return _this.list_filter_mode(''); return function() {
}); return _this.list_filter_mode('');
router.route('active', null, function() { };
return _this.list_filter_mode('active'); })(this));
}); router.route('active', null, (function(_this) {
router.route('completed', null, function() { return function() {
return _this.list_filter_mode('completed'); 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(); Backbone.history.start();
}; };
......
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.7.1
(function() { (function() {
window.TodoViewModel = function(model) { window.TodoViewModel = function(model) {
var ENTER_KEY, var ENTER_KEY, ESCAPE_KEY;
_this = this;
ENTER_KEY = 13; ENTER_KEY = 13;
ESCAPE_KEY = 27;
this.editing = ko.observable(false); this.editing = ko.observable(false);
this.completed = kb.observable(model, { this.completed = kb.observable(model, {
key: 'completed', key: 'completed',
...@@ -17,34 +16,45 @@ ...@@ -17,34 +16,45 @@
}, this); }, this);
this.title = kb.observable(model, { this.title = kb.observable(model, {
key: 'title', key: 'title',
write: (function(title) { write: ((function(_this) {
if ($.trim(title)) { return function(title) {
model.save({ if ($.trim(title)) {
title: $.trim(title) model.save({
}); title: $.trim(title)
} else { });
_.defer(function() { } else {
return model.destroy(); _.defer(function() {
}); return model.destroy();
} });
return _this.editing(false); }
}) return _this.editing(false);
};
})(this))
}, this); }, this);
this.onDestroyTodo = function() { this.onDestroyTodo = (function(_this) {
return model.destroy(); return function() {
}; return model.destroy();
this.onCheckEditBegin = function() { };
if (!_this.editing()) { })(this);
_this.editing(true); this.onCheckEditBegin = (function(_this) {
return $('.todo-input').focus(); return function() {
} if (!_this.editing()) {
}; _this.editing(true);
this.onCheckEditEnd = function(view_model, event) { return $('.todo-input').focus();
if ((event.keyCode === ENTER_KEY) || (event.type === 'blur')) { }
$('.todo-input').blur(); };
return _this.editing(false); })(this);
} this.onCheckEditEnd = (function(_this) {
}; return function(view_model, event) {
if (event.keyCode === ESCAPE_KEY) {
_this.editing(false);
}
if ((event.keyCode === ENTER_KEY) || (event.type === 'blur')) {
$('.todo-input').blur();
return _this.editing(false);
}
};
})(this);
}; };
}).call(this); }).call(this);
window.TodoViewModel = (model) -> window.TodoViewModel = (model) ->
ENTER_KEY = 13 ENTER_KEY = 13
ESCAPE_KEY = 27
@editing = ko.observable(false) @editing = ko.observable(false)
@completed = kb.observable(model, {key: 'completed', read: (-> return model.completed()), write: ((completed) -> model.completed(completed)) }, @) @completed = kb.observable(model, {key: 'completed', read: (-> return model.completed()), write: ((completed) -> model.completed(completed)) }, @)
...@@ -20,8 +21,10 @@ window.TodoViewModel = (model) -> ...@@ -20,8 +21,10 @@ window.TodoViewModel = (model) ->
$('.todo-input').focus() $('.todo-input').focus()
@onCheckEditEnd = (view_model, event) => @onCheckEditEnd = (view_model, event) =>
if (event.keyCode == ESCAPE_KEY)
@editing(false)
if (event.keyCode == ENTER_KEY) or (event.type == 'blur') if (event.keyCode == ENTER_KEY) or (event.type == 'blur')
$('.todo-input').blur() $('.todo-input').blur()
@editing(false) @editing(false)
return return
\ No newline at end of file
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