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
b645c677
Commit
b645c677
authored
Mar 05, 2014
by
Stephen Sawchuk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[angularjs] use module instead of global variable.
parent
ccb41288
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
115 additions
and
106 deletions
+115
-106
architecture-examples/angularjs/js/app.js
architecture-examples/angularjs/js/app.js
+3
-3
architecture-examples/angularjs/js/controllers/todoCtrl.js
architecture-examples/angularjs/js/controllers/todoCtrl.js
+67
-65
architecture-examples/angularjs/js/directives/todoEscape.js
architecture-examples/angularjs/js/directives/todoEscape.js
+15
-12
architecture-examples/angularjs/js/directives/todoFocus.js
architecture-examples/angularjs/js/directives/todoFocus.js
+15
-13
architecture-examples/angularjs/js/services/todoStorage.js
architecture-examples/angularjs/js/services/todoStorage.js
+15
-13
No files found.
architecture-examples/angularjs/js/app.js
View file @
b645c677
/*global angular */
/*jshint unused:false */
'
use strict
'
;
/**
* The main TodoMVC app module
*
* @type {angular.Module}
*/
var
todomvc
=
angular
.
module
(
'
todomvc
'
,
[
'
ngRoute
'
])
angular
.
module
(
'
todomvc
'
,
[
'
ngRoute
'
])
.
config
(
function
(
$routeProvider
)
{
'
use strict
'
;
$routeProvider
.
when
(
'
/
'
,
{
controller
:
'
TodoCtrl
'
,
templateUrl
:
'
todomvc-index.html
'
...
...
architecture-examples/angularjs/js/controllers/todoCtrl.js
View file @
b645c677
/*global todomvc, angular */
'
use strict
'
;
/*global angular */
/**
* The main controller for the app. The controller:
* - retrieves and persists the model via the todoStorage service
* - exposes the model to the template and provides event handlers
*/
todomvc
.
controller
(
'
TodoCtrl
'
,
function
TodoCtrl
(
$scope
,
$routeParams
,
todoStorage
,
filterFilter
)
{
var
todos
=
$scope
.
todos
=
todoStorage
.
get
();
$scope
.
newTodo
=
''
;
$scope
.
editedTodo
=
null
;
$scope
.
$watch
(
'
todos
'
,
function
(
newValue
,
oldValue
)
{
$scope
.
remainingCount
=
filterFilter
(
todos
,
{
completed
:
false
}).
length
;
$scope
.
completedCount
=
todos
.
length
-
$scope
.
remainingCount
;
$scope
.
allChecked
=
!
$scope
.
remainingCount
;
if
(
newValue
!==
oldValue
)
{
// This prevents unneeded calls to the local storage
todoStorage
.
put
(
todos
);
}
},
true
);
// Monitor the current route for changes and adjust the filter accordingly.
$scope
.
$on
(
'
$routeChangeSuccess
'
,
function
()
{
var
status
=
$scope
.
status
=
$routeParams
.
status
||
''
;
$scope
.
statusFilter
=
(
status
===
'
active
'
)
?
{
completed
:
false
}
:
(
status
===
'
completed
'
)
?
{
completed
:
true
}
:
null
;
});
angular
.
module
(
'
todomvc
'
)
.
controller
(
'
TodoCtrl
'
,
function
TodoCtrl
(
$scope
,
$routeParams
,
todoStorage
,
filterFilter
)
{
'
use strict
'
;
var
todos
=
$scope
.
todos
=
todoStorage
.
get
();
$scope
.
addTodo
=
function
()
{
var
newTodo
=
$scope
.
newTodo
.
trim
();
if
(
!
newTodo
.
length
)
{
return
;
}
$scope
.
newTodo
=
''
;
$scope
.
editedTodo
=
null
;
todos
.
push
({
title
:
newTodo
,
completed
:
false
$scope
.
$watch
(
'
todos
'
,
function
(
newValue
,
oldValue
)
{
$scope
.
remainingCount
=
filterFilter
(
todos
,
{
completed
:
false
}).
length
;
$scope
.
completedCount
=
todos
.
length
-
$scope
.
remainingCount
;
$scope
.
allChecked
=
!
$scope
.
remainingCount
;
if
(
newValue
!==
oldValue
)
{
// This prevents unneeded calls to the local storage
todoStorage
.
put
(
todos
);
}
},
true
);
// Monitor the current route for changes and adjust the filter accordingly.
$scope
.
$on
(
'
$routeChangeSuccess
'
,
function
()
{
var
status
=
$scope
.
status
=
$routeParams
.
status
||
''
;
$scope
.
statusFilter
=
(
status
===
'
active
'
)
?
{
completed
:
false
}
:
(
status
===
'
completed
'
)
?
{
completed
:
true
}
:
null
;
});
$scope
.
newTodo
=
''
;
};
$scope
.
addTodo
=
function
()
{
var
newTodo
=
$scope
.
newTodo
.
trim
();
if
(
!
newTodo
.
length
)
{
return
;
}
$scope
.
editTodo
=
function
(
todo
)
{
$scope
.
editedTodo
=
todo
;
// Clone the original todo to restore it on demand.
$scope
.
originalTodo
=
angular
.
extend
({},
todo
);
};
todos
.
push
({
title
:
newTodo
,
completed
:
false
});
$scope
.
doneEditing
=
function
(
todo
)
{
$scope
.
editedTodo
=
null
;
todo
.
title
=
todo
.
title
.
trim
();
$scope
.
newTodo
=
''
;
};
if
(
!
todo
.
title
)
{
$scope
.
removeTodo
(
todo
);
}
};
$scope
.
editTodo
=
function
(
todo
)
{
$scope
.
editedTodo
=
todo
;
// Clone the original todo to restore it on demand.
$scope
.
originalTodo
=
angular
.
extend
({},
todo
);
};
$scope
.
revertEditing
=
function
(
todo
)
{
todos
[
todos
.
indexOf
(
todo
)]
=
$scope
.
originalTodo
;
$scope
.
doneEditing
(
$scope
.
originalTodo
);
};
$scope
.
doneEditing
=
function
(
todo
)
{
$scope
.
editedTodo
=
null
;
todo
.
title
=
todo
.
title
.
trim
();
$scope
.
removeTodo
=
function
(
todo
)
{
todos
.
splice
(
todos
.
indexOf
(
todo
),
1
);
};
if
(
!
todo
.
title
)
{
$scope
.
removeTodo
(
todo
);
}
};
$scope
.
clearCompletedTodos
=
function
()
{
$scope
.
todos
=
todos
=
todos
.
filter
(
function
(
val
)
{
return
!
val
.
completed
;
});
};
$scope
.
revertEditing
=
function
(
todo
)
{
todos
[
todos
.
indexOf
(
todo
)]
=
$scope
.
originalTodo
;
$scope
.
doneEditing
(
$scope
.
originalTodo
);
};
$scope
.
markAll
=
function
(
completed
)
{
todos
.
forEach
(
function
(
todo
)
{
todo
.
completed
=
!
completed
;
});
};
});
$scope
.
removeTodo
=
function
(
todo
)
{
todos
.
splice
(
todos
.
indexOf
(
todo
),
1
);
};
$scope
.
clearCompletedTodos
=
function
()
{
$scope
.
todos
=
todos
=
todos
.
filter
(
function
(
val
)
{
return
!
val
.
completed
;
});
};
$scope
.
markAll
=
function
(
completed
)
{
todos
.
forEach
(
function
(
todo
)
{
todo
.
completed
=
!
completed
;
});
};
});
architecture-examples/angularjs/js/directives/todoEscape.js
View file @
b645c677
/*global todomvc */
'
use strict
'
;
/*global angular */
/**
* Directive that executes an expression when the element it is applied to gets
* an `escape` keydown event.
*/
todomvc
.
directive
(
'
todoEscape
'
,
function
()
{
var
ESCAPE_KEY
=
27
;
return
function
(
scope
,
elem
,
attrs
)
{
elem
.
bind
(
'
keydown
'
,
function
(
event
)
{
if
(
event
.
keyCode
===
ESCAPE_KEY
)
{
scope
.
$apply
(
attrs
.
todoEscape
);
}
});
};
});
angular
.
module
(
'
todomvc
'
)
.
directive
(
'
todoEscape
'
,
function
()
{
'
use strict
'
;
var
ESCAPE_KEY
=
27
;
return
function
(
scope
,
elem
,
attrs
)
{
elem
.
bind
(
'
keydown
'
,
function
(
event
)
{
if
(
event
.
keyCode
===
ESCAPE_KEY
)
{
scope
.
$apply
(
attrs
.
todoEscape
);
}
});
};
});
architecture-examples/angularjs/js/directives/todoFocus.js
View file @
b645c677
/*global todomvc */
'
use strict
'
;
/*global angular */
/**
* Directive that places focus on the element it is applied to when the
* expression it binds to evaluates to true
*/
todomvc
.
directive
(
'
todoFocus
'
,
function
todoFocus
(
$timeout
)
{
return
function
(
scope
,
elem
,
attrs
)
{
scope
.
$watch
(
attrs
.
todoFocus
,
function
(
newVal
)
{
if
(
newVal
)
{
$timeout
(
function
()
{
elem
[
0
].
focus
();
},
0
,
false
);
}
});
};
});
angular
.
module
(
'
todomvc
'
)
.
directive
(
'
todoFocus
'
,
function
todoFocus
(
$timeout
)
{
'
use strict
'
;
return
function
(
scope
,
elem
,
attrs
)
{
scope
.
$watch
(
attrs
.
todoFocus
,
function
(
newVal
)
{
if
(
newVal
)
{
$timeout
(
function
()
{
elem
[
0
].
focus
();
},
0
,
false
);
}
});
};
});
architecture-examples/angularjs/js/services/todoStorage.js
View file @
b645c677
/*global todomvc */
'
use strict
'
;
/*global angular */
/**
* Services that persists and retrieves TODOs from localStorage
*/
todomvc
.
factory
(
'
todoStorage
'
,
function
()
{
var
STORAGE_ID
=
'
todos-angularjs
'
;
angular
.
module
(
'
todomvc
'
)
.
factory
(
'
todoStorage
'
,
function
()
{
'
use strict
'
;
return
{
get
:
function
()
{
return
JSON
.
parse
(
localStorage
.
getItem
(
STORAGE_ID
)
||
'
[]
'
);
},
var
STORAGE_ID
=
'
todos-angularjs
'
;
put
:
function
(
todos
)
{
localStorage
.
setItem
(
STORAGE_ID
,
JSON
.
stringify
(
todos
));
}
};
});
return
{
get
:
function
()
{
return
JSON
.
parse
(
localStorage
.
getItem
(
STORAGE_ID
)
||
'
[]
'
);
},
put
:
function
(
todos
)
{
localStorage
.
setItem
(
STORAGE_ID
,
JSON
.
stringify
(
todos
));
}
};
});
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