Skip to content

Commit

Permalink
Changing redraw strategy while handling input events in order to avoi…
Browse files Browse the repository at this point in the history
…d focus

issues.

Fix style issues: missing spaces.

Fix avoid double deletion when handling empty input and the following blur event.
  • Loading branch information
marcolamberto committed Aug 24, 2015
1 parent c3f5dc5 commit 80f9fbe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
6 changes: 5 additions & 1 deletion examples/mithril/js/controllers/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ app.controller = function () {
this.list = app.storage.get();

// Update with props
this.list = this.list.map(function(item) {
this.list = this.list.map(function (item) {
return new app.Todo(item);
});

Expand Down Expand Up @@ -53,6 +53,10 @@ app.controller = function () {
};

this.doneEditing = function (todo, index) {
if (!todo.editing()) {
return;
}

todo.editing(false);
todo.title(todo.title().trim());
if (!todo.title()) {
Expand Down
17 changes: 12 additions & 5 deletions examples/mithril/js/views/main-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ var app = app || {};
// View utility
app.watchInput = function (onenter, onescape) {
return function (e) {
m.redraw.strategy('none');
if (e.keyCode === app.ENTER_KEY) {
onenter();
m.redraw.strategy('diff');
} else if (e.keyCode === app.ESC_KEY) {
onescape();
m.redraw.strategy('diff');
}
};
};

app.view = (function() {
app.view = (function () {
var focused = false;

return function (ctrl) {
Expand Down Expand Up @@ -63,13 +66,17 @@ app.view = (function() {
})
]), m('input.edit', {
value: task.title(),
onkeyup: app.watchInput(ctrl.doneEditing.bind(ctrl, task, index),
ctrl.cancelEditing.bind(ctrl, task)),
oninput: m.withAttr('value', task.title),
onkeyup: app.watchInput(
ctrl.doneEditing.bind(ctrl, task, index),
ctrl.cancelEditing.bind(ctrl, task)
),
oninput: m.withAttr('value', function (value) {
m.redraw.strategy('none');
task.title(value);
}),
config: function (element) {
if (task.editing()) {
element.focus();
element.selectionStart = element.value.length;
}
},
onblur: ctrl.doneEditing.bind(ctrl, task, index)
Expand Down

0 comments on commit 80f9fbe

Please sign in to comment.