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
9012ec86
Commit
9012ec86
authored
Mar 27, 2015
by
Sam Saccone
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1221 from evverx/direct-delegate
Delegate events to #todo-list instead of document
parents
96638935
822adc1a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
31 deletions
+19
-31
examples/vanillajs/js/helpers.js
examples/vanillajs/js/helpers.js
+12
-24
examples/vanillajs/js/view.js
examples/vanillajs/js/view.js
+7
-7
No files found.
examples/vanillajs/js/helpers.js
View file @
9012ec86
...
...
@@ -15,36 +15,24 @@
target
.
addEventListener
(
type
,
callback
,
!!
useCapture
);
};
// Register events on elements that may or may not exist yet:
// $live('div a', 'click', function (event) {});
window
.
$live
=
(
function
()
{
var
eventRegistry
=
{};
// Attach a handler to event for all elements that match the selector,
// now or in the future, based on a root element
window
.
$delegate
=
function
(
target
,
selector
,
type
,
handler
)
{
function
dispatchEvent
(
event
)
{
var
targetElement
=
event
.
target
;
var
potentialElements
=
window
.
qsa
(
selector
,
target
);
var
hasMatch
=
Array
.
prototype
.
indexOf
.
call
(
potentialElements
,
targetElement
)
>=
0
;
eventRegistry
[
event
.
type
].
forEach
(
function
(
entry
)
{
var
potentialElements
=
window
.
qsa
(
entry
.
selector
);
var
hasMatch
=
Array
.
prototype
.
indexOf
.
call
(
potentialElements
,
targetElement
)
>=
0
;
if
(
hasMatch
)
{
entry
.
handler
.
call
(
targetElement
,
event
);
}
});
if
(
hasMatch
)
{
handler
.
call
(
targetElement
,
event
);
}
}
return
function
(
selector
,
event
,
handler
)
{
if
(
!
eventRegistry
[
event
])
{
eventRegistry
[
event
]
=
[];
window
.
$on
(
document
.
documentElement
,
event
,
dispatchEvent
,
true
);
}
// https://developer.mozilla.org/en-US/docs/Web/Events/blur
var
useCapture
=
type
===
'
blur
'
||
type
===
'
focus
'
;
eventRegistry
[
event
].
push
({
selector
:
selector
,
handler
:
handler
});
};
}());
window
.
$on
(
target
,
type
,
dispatchEvent
,
useCapture
);
};
// Find the element's parent with the given tag name:
// $parent(qs('a'), 'div');
...
...
examples/vanillajs/js/view.js
View file @
9012ec86
/*global qs, qsa, $on, $parent, $
liv
e */
/*global qs, qsa, $on, $parent, $
delegat
e */
(
function
(
window
)
{
'
use strict
'
;
...
...
@@ -140,7 +140,7 @@
View
.
prototype
.
_bindItemEditDone
=
function
(
handler
)
{
var
that
=
this
;
$
live
(
'
#todo-list
li .edit
'
,
'
blur
'
,
function
()
{
$
delegate
(
that
.
$todoList
,
'
li .edit
'
,
'
blur
'
,
function
()
{
if
(
!
this
.
dataset
.
iscanceled
)
{
handler
({
id
:
that
.
_itemId
(
this
),
...
...
@@ -149,7 +149,7 @@
}
});
$
live
(
'
#todo-list
li .edit
'
,
'
keypress
'
,
function
(
event
)
{
$
delegate
(
that
.
$todoList
,
'
li .edit
'
,
'
keypress
'
,
function
(
event
)
{
if
(
event
.
keyCode
===
that
.
ENTER_KEY
)
{
// Remove the cursor from the input when you hit enter just like if it
// were a real form
...
...
@@ -160,7 +160,7 @@
View
.
prototype
.
_bindItemEditCancel
=
function
(
handler
)
{
var
that
=
this
;
$
live
(
'
#todo-list
li .edit
'
,
'
keyup
'
,
function
(
event
)
{
$
delegate
(
that
.
$todoList
,
'
li .edit
'
,
'
keyup
'
,
function
(
event
)
{
if
(
event
.
keyCode
===
that
.
ESCAPE_KEY
)
{
this
.
dataset
.
iscanceled
=
true
;
this
.
blur
();
...
...
@@ -188,17 +188,17 @@
});
}
else
if
(
event
===
'
itemEdit
'
)
{
$
live
(
'
#todo-list
li label
'
,
'
dblclick
'
,
function
()
{
$
delegate
(
that
.
$todoList
,
'
li label
'
,
'
dblclick
'
,
function
()
{
handler
({
id
:
that
.
_itemId
(
this
)});
});
}
else
if
(
event
===
'
itemRemove
'
)
{
$
live
(
'
#todo-list
.destroy
'
,
'
click
'
,
function
()
{
$
delegate
(
that
.
$todoList
,
'
.destroy
'
,
'
click
'
,
function
()
{
handler
({
id
:
that
.
_itemId
(
this
)});
});
}
else
if
(
event
===
'
itemToggle
'
)
{
$
live
(
'
#todo-list
.toggle
'
,
'
click
'
,
function
()
{
$
delegate
(
that
.
$todoList
,
'
.toggle
'
,
'
click
'
,
function
()
{
handler
({
id
:
that
.
_itemId
(
this
),
completed
:
this
.
checked
...
...
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