Commit 9fc18e15 authored by Duilio Protti's avatar Duilio Protti Committed by Sindre Sorhus

Close GH-924: Spine - cancel editing on escape keypress. Fixes #789

parent 5b1af514
// Generated by CoffeeScript 1.6.3 // Generated by CoffeeScript 1.7.1
(function() { (function() {
var TodoApp, var TodoApp,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
...@@ -43,10 +43,10 @@ ...@@ -43,10 +43,10 @@
this.routes({ this.routes({
'/:filter': function(param) { '/:filter': function(param) {
this.filter = param.filter; this.filter = param.filter;
/* /*
TODO: Need to figure out why the route doesn't trigger `change` event TODO: Need to figure out why the route doesn't trigger `change` event
*/ */
Todo.trigger('refresh'); Todo.trigger('refresh');
return this.filters.removeClass('selected').filter("[href='#/" + this.filter + "']").addClass('selected'); return this.filters.removeClass('selected').filter("[href='#/" + this.filter + "']").addClass('selected');
} }
...@@ -97,11 +97,11 @@ ...@@ -97,11 +97,11 @@
TodoApp.prototype.toggleAll = function(e) { TodoApp.prototype.toggleAll = function(e) {
return Todo.each(function(todo) { return Todo.each(function(todo) {
/* /*
TODO: Model updateAttribute sometimes won't stick: TODO: Model updateAttribute sometimes won't stick:
https://github.com/maccman/spine/issues/219 https://github.com/maccman/spine/issues/219
*/ */
todo.updateAttribute('completed', e.target.checked); todo.updateAttribute('completed', e.target.checked);
return todo.trigger('update', todo); return todo.trigger('update', todo);
}); });
......
// Generated by CoffeeScript 1.6.3 // Generated by CoffeeScript 1.7.1
(function() { (function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty, __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.Todos = (function(_super) { window.Todos = (function(_super) {
var ENTER_KEY, TPL; var ENTER_KEY, ESCAPE_KEY, TPL;
__extends(Todos, _super); __extends(Todos, _super);
ENTER_KEY = 13; ENTER_KEY = 13;
ESCAPE_KEY = 27;
TPL = Handlebars.compile($('#todo-template').html()); TPL = Handlebars.compile($('#todo-template').html());
Todos.prototype.elements = { Todos.prototype.elements = {
...@@ -21,6 +23,7 @@ ...@@ -21,6 +23,7 @@
'click .destroy': 'remove', 'click .destroy': 'remove',
'click .toggle': 'toggleStatus', 'click .toggle': 'toggleStatus',
'dblclick label': 'edit', 'dblclick label': 'edit',
'keydown .edit': 'revertEditOnEscape',
'keyup .edit': 'finishEditOnEnter', 'keyup .edit': 'finishEditOnEnter',
'blur .edit': 'finishEdit' 'blur .edit': 'finishEdit'
}; };
...@@ -67,6 +70,17 @@ ...@@ -67,6 +70,17 @@
} }
}; };
Todos.prototype.revertEdit = function() {
this.el.removeClass('editing');
return this.editElem.val(this.todo.title);
};
Todos.prototype.revertEditOnEscape = function(e) {
if (e.which === ESCAPE_KEY) {
return this.revertEdit();
}
};
return Todos; return Todos;
})(Spine.Controller); })(Spine.Controller);
......
// Generated by CoffeeScript 1.6.3 // Generated by CoffeeScript 1.7.1
(function() { (function() {
var _ref, var __hasProp = {}.hasOwnProperty,
__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() {
_ref = Todo.__super__.constructor.apply(this, arguments); return Todo.__super__.constructor.apply(this, arguments);
return _ref;
} }
Todo.configure('Todo', 'title', 'completed'); Todo.configure('Todo', 'title', 'completed');
...@@ -29,11 +27,11 @@ ...@@ -29,11 +27,11 @@
}; };
Todo.destroyCompleted = function() { Todo.destroyCompleted = function() {
var todo, _i, _len, _ref1, _results; var todo, _i, _len, _ref, _results;
_ref1 = this.completed(); _ref = this.completed();
_results = []; _results = [];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
todo = _ref1[_i]; todo = _ref[_i];
_results.push(todo.destroy()); _results.push(todo.destroy());
} }
return _results; return _results;
......
class window.Todos extends Spine.Controller class window.Todos extends Spine.Controller
ENTER_KEY = 13 ENTER_KEY = 13
ESCAPE_KEY = 27
TPL = Handlebars.compile $('#todo-template').html() TPL = Handlebars.compile $('#todo-template').html()
elements: elements:
...@@ -9,6 +10,7 @@ class window.Todos extends Spine.Controller ...@@ -9,6 +10,7 @@ class window.Todos extends Spine.Controller
'click .destroy': 'remove' 'click .destroy': 'remove'
'click .toggle': 'toggleStatus' 'click .toggle': 'toggleStatus'
'dblclick label': 'edit' 'dblclick label': 'edit'
'keydown .edit': 'revertEditOnEscape'
'keyup .edit': 'finishEditOnEnter' 'keyup .edit': 'finishEditOnEnter'
'blur .edit': 'finishEdit' 'blur .edit': 'finishEdit'
...@@ -38,3 +40,11 @@ class window.Todos extends Spine.Controller ...@@ -38,3 +40,11 @@ class window.Todos extends Spine.Controller
finishEditOnEnter: (e) -> finishEditOnEnter: (e) ->
@finishEdit() if e.which is ENTER_KEY @finishEdit() if e.which is ENTER_KEY
revertEdit: ->
@el.removeClass 'editing'
@editElem.val(@todo.title)
revertEditOnEscape: (e) ->
@revertEdit() if e.which is ESCAPE_KEY
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