Commit cd32675d authored by Dave Methvin's avatar Dave Methvin

thorax: Cancel editing with the Escape key

parent 69cb6f7c
/*global Thorax, Backbone*/ /*global Thorax, Backbone*/
/*jshint unused:false*/ /*jshint unused:false*/
var ENTER_KEY = 13; var ENTER_KEY = 13;
var ESCAPE_KEY = 27;
(function () { (function () {
'use strict'; 'use strict';
......
/*global Thorax, ENTER_KEY*/ /*global Thorax, ENTER_KEY, ESCAPE_KEY*/
(function () { (function () {
'use strict'; 'use strict';
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
'click .toggle': 'toggleCompleted', 'click .toggle': 'toggleCompleted',
'dblclick label': 'edit', 'dblclick label': 'edit',
'click .destroy': 'clear', 'click .destroy': 'clear',
'keypress .edit': 'updateOnEnter', 'keyup .edit': 'keyListener',
'blur .edit': 'close', 'blur .edit': 'close',
// The "rendered" event is triggered by Thorax each time render() // The "rendered" event is triggered by Thorax each time render()
// is called and the result of the template has been appended // is called and the result of the template has been appended
...@@ -37,11 +37,16 @@ ...@@ -37,11 +37,16 @@
// Switch this view into `"editing"` mode, displaying the input field. // Switch this view into `"editing"` mode, displaying the input field.
edit: function () { edit: function () {
this.$el.addClass('editing'); this.$el.addClass('editing');
this.$('.edit').focus(); this.$('.edit').val(this.model.get('title')).focus();
}, },
// Close the `"editing"` mode, saving changes to the todo. // Close the `"editing"` mode.
close: function () { close: function () {
// If editing was cancelled, don't save
if (!this.$el.hasClass('editing')) {
return;
}
var value = this.$('.edit').val().trim(); var value = this.$('.edit').val().trim();
if (value) { if (value) {
...@@ -53,10 +58,17 @@ ...@@ -53,10 +58,17 @@
this.$el.removeClass('editing'); this.$el.removeClass('editing');
}, },
// If you hit `enter`, we're through editing the item. // User cancelled editing, don't update the todo.
updateOnEnter: function (e) { cancelEdits: function () {
this.$el.removeClass('editing');
},
// Enter completes the editing, Escape cancels it
keyListener: function (e) {
if (e.which === ENTER_KEY) { if (e.which === ENTER_KEY) {
this.close(); this.close();
} else if (e.which === ESCAPE_KEY) {
this.cancelEdits();
} }
}, },
......
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