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
Eugene Shen
todomvc
Commits
b0411a67
Commit
b0411a67
authored
Mar 09, 2013
by
Sindre Sorhus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
jQuery app - code style
parent
ae08e0e2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
66 deletions
+89
-66
architecture-examples/jquery/index.html
architecture-examples/jquery/index.html
+50
-50
architecture-examples/jquery/js/app.js
architecture-examples/jquery/js/app.js
+39
-16
No files found.
architecture-examples/jquery/index.html
View file @
b0411a67
<!doctype html>
<!doctype html>
<html
lang=
"en"
>
<html
lang=
"en"
>
<head>
<head>
<meta
charset=
"utf-8"
>
<meta
charset=
"utf-8"
>
<meta
http-equiv=
"X-UA-Compatible"
content=
"IE=edge,chrome=1"
>
<meta
http-equiv=
"X-UA-Compatible"
content=
"IE=edge,chrome=1"
>
<title>
jQuery • TodoMVC
</title>
<title>
jQuery • TodoMVC
</title>
<link
rel=
"stylesheet"
href=
"../../assets/base.css"
>
<link
rel=
"stylesheet"
href=
"../../assets/base.css"
>
<link
rel=
"stylesheet"
href=
"css/app.css"
>
<link
rel=
"stylesheet"
href=
"css/app.css"
>
<!--[if IE]>
<!--[if IE]>
<script src="../../assets/ie.js"></script>
<script src="../../assets/ie.js"></script>
<![endif]-->
<![endif]-->
</head>
</head>
<body>
<body>
<section
id=
"todoapp"
>
<section
id=
"todoapp"
>
<header
id=
"header"
>
<header
id=
"header"
>
<h1>
todos
</h1>
<h1>
todos
</h1>
<input
id=
"new-todo"
placeholder=
"What needs to be done?"
autofocus
>
<input
id=
"new-todo"
placeholder=
"What needs to be done?"
autofocus
>
</header>
</header>
<section
id=
"main"
>
<section
id=
"main"
>
<input
id=
"toggle-all"
type=
"checkbox"
>
<input
id=
"toggle-all"
type=
"checkbox"
>
<label
for=
"toggle-all"
>
Mark all as complete
</label>
<label
for=
"toggle-all"
>
Mark all as complete
</label>
<ul
id=
"todo-list"
></ul>
<ul
id=
"todo-list"
></ul>
</section>
<footer
id=
"footer"
>
<span
id=
"todo-count"
><strong>
0
</strong>
item left
</span>
<button
id=
"clear-completed"
>
Clear completed
</button>
</footer>
</section>
</section>
<footer
id=
"footer"
>
<footer
id=
"info"
>
<span
id=
"todo-count"
><strong>
0
</strong>
item left
</span>
<p>
Double-click to edit a todo
</p>
<button
id=
"clear-completed"
>
Clear completed
</button>
<p>
Created by
<a
href=
"http://github.com/sindresorhus"
>
Sindre Sorhus
</a></p>
<p>
Part of
<a
href=
"http://todomvc.com"
>
TodoMVC
</a></p>
</footer>
</footer>
</section>
<script
id=
"todo-template"
type=
"text/x-handlebars-template"
>
<footer
id=
"info"
>
{{
#
this
}}
<p>
Double-click to edit a todo
</p>
<
li
{{
#
if
completed
}}
class
=
"
completed
"
{{
/if}} data-id="{{id}}"
>
<p>
Created by
<a
href=
"http://github.com/sindresorhus"
>
Sindre Sorhus
</a></p>
<
div
class
=
"
view
"
>
<p>
Part of
<a
href=
"http://todomvc.com"
>
TodoMVC
</a></p>
<
input
class
=
"
toggle
"
type
=
"
checkbox
"
{{
#
if
completed
}}
checked
{{
/if}}
>
</footer>
<
label
>
{{
title
}}
<
/label
>
<script
id=
"todo-template"
type=
"text/x-handlebars-template"
>
<
button
class
=
"
destroy
"
><
/button
>
{{
#
this
}}
<
/div
>
<
li
{{
#
if
completed
}}
class
=
"
completed
"
{{
/if}} data-id="{{id}}"
>
<
input
class
=
"
edit
"
value
=
"
{{title}}
"
>
<
div
class
=
"
view
"
>
<
/li
>
<
input
class
=
"
toggle
"
type
=
"
checkbox
"
{{
#
if
completed
}}
checked
{{
/if}}
>
{{
/
this
}}
<
label
>
{{
title
}}
<
/label
>
</script>
<
button
class
=
"
destroy
"
><
/button
>
<script
id=
"footer-template"
type=
"text/x-handlebars-template"
>
<
/div
>
<
span
id
=
"
todo-count
"
><
strong
>
{{
activeTodoCount
}}
<
/strong> {{activeTodoWord}} left</
span
>
<
input
class
=
"
edit
"
value
=
"
{{title}}
"
>
{{
#
if
completedTodos
}}
<
button
id
=
"
clear-completed
"
>
Clear
completed
({{
completedTodos
}})
<
/button>{{/i
f
}}
<
/li
>
</script>
{{
/
this
}}
<script
src=
"../../assets/base.js"
></script>
</script>
<script
src=
"../../assets/jquery.min.js"
></script>
<script
id=
"footer-template"
type=
"text/x-handlebars-template"
>
<script
src=
"../../assets/handlebars.min.js"
></script>
<
span
id
=
"
todo-count
"
><
strong
>
{{
activeTodoCount
}}
<
/strong> {{activeTodoWord}} left</
span
>
<script
src=
"js/app.js"
></script>
{{
#
if
completedTodos
}}
<
button
id
=
"
clear-completed
"
>
Clear
completed
({{
completedTodos
}})
<
/button>{{/i
f
}}
</body>
</script>
<script
src=
"../../assets/base.js"
></script>
<script
src=
"../../assets/jquery.min.js"
></script>
<script
src=
"../../assets/handlebars.min.js"
></script>
<script
src=
"js/app.js"
></script>
</body>
</html>
</html>
architecture-examples/jquery/js/app.js
View file @
b0411a67
...
@@ -3,8 +3,21 @@ jQuery(function ($) {
...
@@ -3,8 +3,21 @@ jQuery(function ($) {
'
use strict
'
;
'
use strict
'
;
var
Utils
=
{
var
Utils
=
{
// https://gist.github.com/1308368
uuid
:
function
()
{
uuid
:
function
(
a
,
b
){
for
(
b
=
a
=
''
;
a
++<
36
;
b
+=
a
*
51
&
52
?(
a
^
15
?
8
^
Math
.
random
()
*
(
a
^
20
?
16
:
4
):
4
).
toString
(
16
):
'
-
'
);
return
b
},
/*jshint bitwise:false */
var
i
,
random
;
var
uuid
=
''
;
for
(
i
=
0
;
i
<
32
;
i
++
)
{
random
=
Math
.
random
()
*
16
|
0
;
if
(
i
===
8
||
i
===
12
||
i
===
16
||
i
===
20
)
{
uuid
+=
'
-
'
;
}
uuid
+=
(
i
===
12
?
4
:
(
i
===
16
?
(
random
&
3
|
8
)
:
random
)).
toString
(
16
);
}
return
uuid
;
},
pluralize
:
function
(
count
,
word
)
{
pluralize
:
function
(
count
,
word
)
{
return
count
===
1
?
word
:
word
+
'
s
'
;
return
count
===
1
?
word
:
word
+
'
s
'
;
},
},
...
@@ -57,47 +70,54 @@ jQuery(function ($) {
...
@@ -57,47 +70,54 @@ jQuery(function ($) {
Utils
.
store
(
'
todos-jquery
'
,
this
.
todos
);
Utils
.
store
(
'
todos-jquery
'
,
this
.
todos
);
},
},
renderFooter
:
function
()
{
renderFooter
:
function
()
{
var
todoCount
=
this
.
todos
.
length
,
var
todoCount
=
this
.
todos
.
length
;
activeTodoCount
=
this
.
activeTodoCount
(),
var
activeTodoCount
=
this
.
activeTodoCount
();
footer
=
{
var
footer
=
{
activeTodoCount
:
activeTodoCount
,
activeTodoCount
:
activeTodoCount
,
activeTodoWord
:
Utils
.
pluralize
(
activeTodoCount
,
'
item
'
),
activeTodoWord
:
Utils
.
pluralize
(
activeTodoCount
,
'
item
'
),
completedTodos
:
todoCount
-
activeTodoCount
completedTodos
:
todoCount
-
activeTodoCount
};
};
this
.
$footer
.
toggle
(
!!
todoCount
);
this
.
$footer
.
toggle
(
!!
todoCount
);
this
.
$footer
.
html
(
this
.
footerTemplate
(
footer
));
this
.
$footer
.
html
(
this
.
footerTemplate
(
footer
));
},
},
toggleAll
:
function
()
{
toggleAll
:
function
()
{
var
isChecked
=
$
(
this
).
prop
(
'
checked
'
);
var
isChecked
=
$
(
this
).
prop
(
'
checked
'
);
$
.
each
(
App
.
todos
,
function
(
i
,
val
)
{
$
.
each
(
App
.
todos
,
function
(
i
,
val
)
{
val
.
completed
=
isChecked
;
val
.
completed
=
isChecked
;
});
});
App
.
render
();
App
.
render
();
},
},
activeTodoCount
:
function
()
{
activeTodoCount
:
function
()
{
var
count
=
0
;
var
count
=
0
;
$
.
each
(
this
.
todos
,
function
(
i
,
val
)
{
$
.
each
(
this
.
todos
,
function
(
i
,
val
)
{
if
(
!
val
.
completed
)
{
if
(
!
val
.
completed
)
{
count
++
;
count
++
;
}
}
});
});
return
count
;
return
count
;
},
},
destroyCompleted
:
function
()
{
destroyCompleted
:
function
()
{
var
todos
=
App
.
todos
,
var
todos
=
App
.
todos
;
l
=
todos
.
length
;
var
l
=
todos
.
length
;
while
(
l
--
)
{
while
(
l
--
)
{
if
(
todos
[
l
].
completed
)
{
if
(
todos
[
l
].
completed
)
{
todos
.
splice
(
l
,
1
);
todos
.
splice
(
l
,
1
);
}
}
}
}
App
.
render
();
App
.
render
();
},
},
//
Accepts an element from inside the ".item"
div and
//
accepts an element from inside the `.item`
div and
// returns the corresponding todo in the todos array
// returns the corresponding todo in the todos array
getTodo
:
function
(
elem
,
callback
)
{
getTodo
:
function
(
elem
,
callback
)
{
var
id
=
$
(
elem
).
closest
(
'
li
'
).
data
(
'
id
'
);
var
id
=
$
(
elem
).
closest
(
'
li
'
).
data
(
'
id
'
);
$
.
each
(
this
.
todos
,
function
(
i
,
val
)
{
$
.
each
(
this
.
todos
,
function
(
i
,
val
)
{
if
(
val
.
id
===
id
)
{
if
(
val
.
id
===
id
)
{
callback
.
apply
(
App
,
arguments
);
callback
.
apply
(
App
,
arguments
);
...
@@ -106,16 +126,19 @@ jQuery(function ($) {
...
@@ -106,16 +126,19 @@ jQuery(function ($) {
});
});
},
},
create
:
function
(
e
)
{
create
:
function
(
e
)
{
var
$input
=
$
(
this
),
var
$input
=
$
(
this
);
val
=
$
.
trim
(
$input
.
val
());
var
val
=
$
.
trim
(
$input
.
val
());
if
(
e
.
which
!==
App
.
ENTER_KEY
||
!
val
)
{
if
(
e
.
which
!==
App
.
ENTER_KEY
||
!
val
)
{
return
;
return
;
}
}
App
.
todos
.
push
({
App
.
todos
.
push
({
id
:
Utils
.
uuid
(),
id
:
Utils
.
uuid
(),
title
:
val
,
title
:
val
,
completed
:
false
completed
:
false
});
});
$input
.
val
(
''
);
$input
.
val
(
''
);
App
.
render
();
App
.
render
();
},
},
...
@@ -129,12 +152,13 @@ jQuery(function ($) {
...
@@ -129,12 +152,13 @@ jQuery(function ($) {
$
(
this
).
closest
(
'
li
'
).
addClass
(
'
editing
'
).
find
(
'
.edit
'
).
focus
();
$
(
this
).
closest
(
'
li
'
).
addClass
(
'
editing
'
).
find
(
'
.edit
'
).
focus
();
},
},
blurOnEnter
:
function
(
e
)
{
blurOnEnter
:
function
(
e
)
{
if
(
e
.
keyCode
===
App
.
ENTER_KEY
)
{
if
(
e
.
which
===
App
.
ENTER_KEY
)
{
e
.
target
.
blur
();
e
.
target
.
blur
();
}
}
},
},
update
:
function
()
{
update
:
function
()
{
var
val
=
$
.
trim
(
$
(
this
).
removeClass
(
'
editing
'
).
val
());
var
val
=
$
.
trim
(
$
(
this
).
removeClass
(
'
editing
'
).
val
());
App
.
getTodo
(
this
,
function
(
i
)
{
App
.
getTodo
(
this
,
function
(
i
)
{
if
(
val
)
{
if
(
val
)
{
this
.
todos
[
i
].
title
=
val
;
this
.
todos
[
i
].
title
=
val
;
...
@@ -153,5 +177,4 @@ jQuery(function ($) {
...
@@ -153,5 +177,4 @@ jQuery(function ($) {
};
};
App
.
init
();
App
.
init
();
});
});
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