Commit 9295d162 authored by Stephen Sawchuk's avatar Stephen Sawchuk

(durandel) code style.

parent 306c0215
...@@ -70,6 +70,7 @@ ...@@ -70,6 +70,7 @@
}, },
update: function (element, valueAccessor) { update: function (element, valueAccessor) {
ko.utils.unwrapObservable(valueAccessor()); // for dependency ko.utils.unwrapObservable(valueAccessor()); // for dependency
// ensure that element is visible before trying to focus // ensure that element is visible before trying to focus
setTimeout(function () { setTimeout(function () {
ko.bindingHandlers.hasfocus.update(element, valueAccessor); ko.bindingHandlers.hasfocus.update(element, valueAccessor);
...@@ -77,11 +78,12 @@ ...@@ -77,11 +78,12 @@
} }
}; };
define(['bower_components/durandal/app', define([
'bower_components/durandal/app',
'bower_components/durandal/viewLocator', 'bower_components/durandal/viewLocator',
'bower_components/durandal/system', 'bower_components/durandal/system',
'bower_components/durandal/plugins/router'], 'bower_components/durandal/plugins/router'
function (app, viewLocator, system, router) { ], function (app, viewLocator, system, router) {
//>>excludeStart("build", true); //>>excludeStart("build", true);
system.debug(true); system.debug(true);
...@@ -89,6 +91,7 @@ ...@@ -89,6 +91,7 @@
// this ensures that the title is simply the caption provided on the route // this ensures that the title is simply the caption provided on the route
app.title = undefined; app.title = undefined;
app.start().then(function () { app.start().then(function () {
// Replace 'viewmodels' in the moduleId with 'views' to locate the view. // Replace 'viewmodels' in the moduleId with 'views' to locate the view.
// Look for partial views in a 'views' folder in the root. // Look for partial views in a 'views' folder in the root.
...@@ -96,6 +99,7 @@ ...@@ -96,6 +99,7 @@
// configure routing // configure routing
router.useConvention('js/viewmodels'); router.useConvention('js/viewmodels');
router.mapNav({ router.mapNav({
url: '/', url: '/',
moduleId: 'js/viewmodels/todoapp', moduleId: 'js/viewmodels/todoapp',
...@@ -117,4 +121,4 @@ ...@@ -117,4 +121,4 @@
app.setRoot('js/viewmodels/shell'); app.setRoot('js/viewmodels/shell');
}); });
}); });
}()); })();
\ No newline at end of file \ No newline at end of file
...@@ -2,17 +2,18 @@ ...@@ -2,17 +2,18 @@
define([ define([
'bower_components/durandal/app' 'bower_components/durandal/app'
], function (app) { ], function (app) {
'use strict'; 'use strict';
var ViewModel = function () { var ViewModel = function () {
var self = this; var self = this;
// store the new todo value being entered // store the new todo value being entered
self.current = ko.observable(); self.current = ko.observable();
// add a new todo, when enter key is pressed // add a new todo, when enter key is pressed
self.add = function () { self.add = function () {
var current = self.current().trim(); var current = self.current().trim();
if (current) { if (current) {
app.trigger('todoitem', current); app.trigger('todoitem', current);
self.current(''); self.current('');
......
...@@ -3,7 +3,6 @@ define([ ...@@ -3,7 +3,6 @@ define([
'bower_components/durandal/app', 'bower_components/durandal/app',
'js/viewmodels/shell' 'js/viewmodels/shell'
], function (app, shell) { ], function (app, shell) {
'use strict'; 'use strict';
// represent a single todo item // represent a single todo item
...@@ -34,13 +33,16 @@ define([ ...@@ -34,13 +33,16 @@ define([
self.todos(todosFromlocalStorage); self.todos(todosFromlocalStorage);
// internal computed observable that fires whenever anything changes in our todos // internal computed observable that fires whenever anything changes in
// our todos
ko.computed(function () { ko.computed(function () {
// store a clean copy to local storage, which also creates a dependency on the observableArray and all observables in each item // store a clean copy to local storage, which also creates a dependency
// on the observableArray and all observables in each item
localStorage.setItem('todos-durandal', ko.toJSON(self.todos())); localStorage.setItem('todos-durandal', ko.toJSON(self.todos()));
}).extend({ }).extend({
// save at most twice per second
throttle: 500 throttle: 500
}); // save at most twice per second });
}; };
...@@ -75,7 +77,8 @@ define([ ...@@ -75,7 +77,8 @@ define([
self.todos.remove(todo); self.todos.remove(todo);
}; };
self.editTitle = ko.observable(''); // shadow variable so that the edit can be discarded on escape // shadow variable so that the edit can be discarded on escape
self.editTitle = ko.observable('');
self.cancelEdit = false; self.cancelEdit = false;
// remove all completed todos // remove all completed todos
...@@ -106,7 +109,7 @@ define([ ...@@ -106,7 +109,7 @@ define([
if (!self.cancelEdit) { if (!self.cancelEdit) {
self.itemBeingEdited(undefined); self.itemBeingEdited(undefined);
//trim and save back // trim and save back
var trimmed = self.editTitle().trim(); var trimmed = self.editTitle().trim();
item.title(trimmed); item.title(trimmed);
...@@ -130,14 +133,16 @@ define([ ...@@ -130,14 +133,16 @@ define([
// writeable computed observable to handle marking all complete/incomplete // writeable computed observable to handle marking all complete/incomplete
self.allCompleted = ko.computed({ self.allCompleted = ko.computed({
//always return true/false based on the done flag of all todos // always return true/false based on the done flag of all todos
read: function () { read: function () {
return !self.remainingCount(); return !self.remainingCount();
}, },
// set all todos to the written value (true/false) // set all todos to the written value (true/false)
write: function (newValue) { write: function (newValue) {
ko.utils.arrayForEach(self.todos(), function (todo) { ko.utils.arrayForEach(self.todos(), function (todo) {
// set even if value is the same, as subscribers are not notified in that case // set even if value is the same, as subscribers are not notified in
// that case
todo.completed(newValue); todo.completed(newValue);
}); });
} }
...@@ -147,9 +152,7 @@ define([ ...@@ -147,9 +152,7 @@ define([
self.getLabel = function (count) { self.getLabel = function (count) {
return ko.utils.unwrapObservable(count) === 1 ? 'item' : 'items'; return ko.utils.unwrapObservable(count) === 1 ? 'item' : 'items';
}; };
}; };
return ViewModel; return ViewModel;
}); });
/*global define*/ /*global define */
define([ define([
'bower_components/durandal/plugins/router', 'bower_components/durandal/plugins/router',
], function (router) { ], function (router) {
'use strict'; 'use strict';
return { return {
router: router, router: router,
filter: undefined, // this is used as the global cache to figure out the filter in effect.
// this is used as the global cache to figure out the filter in effect.
filter: undefined,
activate: function () { activate: function () {
return router.activate('todoapp'); return router.activate('todoapp');
} }
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<footer id="footer" data-bind="visible: completedCount() || remainingCount()"> <footer id="footer" data-bind="visible: completedCount() || remainingCount()">
<span id="todo-count"> <span id="todo-count">
<strong data-bind="text: remainingCount">0</strong> <strong data-bind="text: remainingCount">0</strong>
<span data-bind="text: getLabel( remainingCount )"></span> left <span data-bind="text: getLabel(remainingCount)"></span> left
</span> </span>
<ul id="filters"> <ul id="filters">
<li> <li>
......
...@@ -31,4 +31,4 @@ _If you have other helpful links to share, or find any of the links above no lon ...@@ -31,4 +31,4 @@ _If you have other helpful links to share, or find any of the links above no lon
## Credit ## Credit
This Durandal TodoMVC application was created by [Abhinav Gujjar](https://github.com/abhinavgujjar). This TodoMVC application was created by [Abhinav Gujjar](https://github.com/abhinavgujjar).
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment