Skip to content

Commit 73ebf51

Browse files
committed
Remove todo item, if edited item is blank or just whitespace
1 parent 53d5a33 commit 73ebf51

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

architecture-examples/knockoutjs/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ <h1>Todos</h1>
1717
<label for="toggle-all">Mark all as complete</label>
1818
<ul id="todo-list" data-bind="foreach: todos">
1919
<li data-bind="css: { done: done, editing: editing }">
20-
<div class="view" data-bind="event: { dblclick: edit }">
20+
<div class="view" data-bind="event: { dblclick: $root.editItem }">
2121
<input class="toggle" type="checkbox" data-bind="checked: done">
2222
<label data-bind="text: content"></label>
2323
<a class="destroy" href="#" data-bind="click: $root.remove"></a>
2424
</div>
2525
<input class="edit" type="text"
26-
data-bind="value: content, valueUpdate: 'afterkeydown', enterKey: stopEditing, selectAndFocus: editing, event: { blur: stopEditing }"/>
26+
data-bind="value: content, valueUpdate: 'afterkeydown', enterKey: $root.stopEditing, selectAndFocus: editing, event: { blur: $root.stopEditing }"/>
2727
</li>
2828
</ul>
2929
</section>

architecture-examples/knockoutjs/js/app.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@
6868
this.editing = ko.observable(false);
6969
};
7070

71-
//can place methods on prototype, as there can be many todos
72-
ko.utils.extend(Todo.prototype, {
73-
edit:function () {
74-
this.editing(true);
75-
},
76-
stopEditing:function () {
77-
this.editing(false);
78-
}
79-
});
80-
8171
//our main view model
8272
var ViewModel = function (todos) {
8373
var self = this;
@@ -110,6 +100,19 @@
110100
return todo.done();
111101
});
112102
};
103+
104+
//edit an item
105+
self.editItem = function(item) {
106+
item.editing(true);
107+
};
108+
109+
//stop editing an item. Remove the item, if it is now empty
110+
self.stopEditing = function(item) {
111+
item.editing(false);
112+
if (!item.content().trim()) {
113+
self.remove(item);
114+
}
115+
};
113116

114117
//count of all completed todos
115118
self.completedCount = ko.computed(function () {

0 commit comments

Comments
 (0)