Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
todomvc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Sven Franck
todomvc
Commits
233db94c
Commit
233db94c
authored
Nov 16, 2013
by
Pascal Hartig
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Knockout.js: cancel on Escape
parent
48a00e7b
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
4265 additions
and
117 deletions
+4265
-117
architecture-examples/knockoutjs/bower_components/knockout.js/knockout.debug.js
...knockoutjs/bower_components/knockout.js/knockout.debug.js
+4225
-0
architecture-examples/knockoutjs/bower_components/knockout.js/knockout.js
...mples/knockoutjs/bower_components/knockout.js/knockout.js
+0
-94
architecture-examples/knockoutjs/index.html
architecture-examples/knockoutjs/index.html
+1
-1
architecture-examples/knockoutjs/js/app.js
architecture-examples/knockoutjs/js/app.js
+39
-22
No files found.
architecture-examples/knockoutjs/bower_components/knockout.js/knockout.debug.js
0 → 100644
View file @
233db94c
This source diff could not be displayed because it is too large. You can
view the blob
instead.
architecture-examples/knockoutjs/bower_components/knockout.js/knockout.js
deleted
100644 → 0
View file @
48a00e7b
This diff is collapsed.
Click to expand it.
architecture-examples/knockoutjs/index.html
View file @
233db94c
...
...
@@ -22,7 +22,7 @@
<label
data-bind=
"text: title, event: { dblclick: $root.editItem }"
></label>
<button
class=
"destroy"
data-bind=
"click: $root.remove"
></button>
</div>
<input
class=
"edit"
data-bind=
"value: title, valueUpdate: 'afterkeydown', enterKey: $root.s
top
Editing, selectAndFocus: editing, event: { blur: $root.stopEditing }"
>
<input
class=
"edit"
data-bind=
"value: title, valueUpdate: 'afterkeydown', enterKey: $root.s
aveEditing, escapeKey: $root.cancel
Editing, selectAndFocus: editing, event: { blur: $root.stopEditing }"
>
</li>
</ul>
</section>
...
...
architecture-examples/knockoutjs/js/app.js
View file @
233db94c
...
...
@@ -3,30 +3,40 @@
'
use strict
'
;
var
ENTER_KEY
=
13
;
var
ESCAPE_KEY
=
27
;
// A factory function we can use to create binding handlers for specific
// keycodes.
function
keyhandlerBindingFactory
(
keyCode
)
{
return
{
init
:
function
(
element
,
valueAccessor
,
allBindingsAccessor
,
data
,
bindingContext
)
{
var
wrappedHandler
,
newValueAccessor
;
// wrap the handler with a check for the enter key
wrappedHandler
=
function
(
data
,
event
)
{
if
(
event
.
keyCode
===
keyCode
)
{
valueAccessor
().
call
(
this
,
data
,
event
);
}
};
// a custom binding to handle the enter key (could go in a separate library)
ko
.
bindingHandlers
.
enterKey
=
{
init
:
function
(
element
,
valueAccessor
,
allBindingsAccessor
,
data
,
bindingContext
)
{
var
wrappedHandler
,
newValueAccessor
;
// wrap the handler with a check for the enter key
wrappedHandler
=
function
(
data
,
event
)
{
if
(
event
.
keyCode
===
ENTER_KEY
)
{
valueAccessor
().
call
(
this
,
data
,
event
);
}
};
// create a valueAccessor with the options that we would want to pass to the event binding
newValueAccessor
=
function
()
{
return
{
keyup
:
wrappedHandler
// create a valueAccessor with the options that we would want to pass to the event binding
newValueAccessor
=
function
()
{
return
{
keyup
:
wrappedHandler
};
};
};
// call the real event binding's init function
ko
.
bindingHandlers
.
event
.
init
(
element
,
newValueAccessor
,
allBindingsAccessor
,
data
,
bindingContext
);
}
};
// call the real event binding's init function
ko
.
bindingHandlers
.
event
.
init
(
element
,
newValueAccessor
,
allBindingsAccessor
,
data
,
bindingContext
);
}
};
}
// a custom binding to handle the enter key
ko
.
bindingHandlers
.
enterKey
=
keyhandlerBindingFactory
(
ENTER_KEY
);
// another custom binding, this time to handle the escape key
ko
.
bindingHandlers
.
escapeKey
=
keyhandlerBindingFactory
(
ESCAPE_KEY
);
// wrapper to hasFocus that also selects text and applies focus async
ko
.
bindingHandlers
.
selectAndFocus
=
{
...
...
@@ -103,10 +113,11 @@
// edit an item
this
.
editItem
=
function
(
item
)
{
item
.
editing
(
true
);
item
.
previousTitle
=
item
.
title
();
}.
bind
(
this
);
// stop editing an item. Remove the item, if it is now empty
this
.
s
top
Editing
=
function
(
item
)
{
this
.
s
ave
Editing
=
function
(
item
)
{
item
.
editing
(
false
);
var
title
=
item
.
title
();
...
...
@@ -124,6 +135,12 @@
}
}.
bind
(
this
);
// cancel editing an item and revert to the previous content
this
.
cancelEditing
=
function
(
item
)
{
item
.
editing
(
false
);
item
.
title
(
item
.
previousTitle
);
}.
bind
(
this
);
// count of all completed todos
this
.
completedCount
=
ko
.
computed
(
function
()
{
return
this
.
todos
().
filter
(
function
(
todo
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment