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

(durandel) code style.

parent 306c0215
......@@ -69,19 +69,21 @@
});
},
update: function (element, valueAccessor) {
ko.utils.unwrapObservable(valueAccessor()); // for dependency
// ensure that element is visible before trying to focus
setTimeout(function () {
ko.bindingHandlers.hasfocus.update(element, valueAccessor);
}, 0);
}
ko.utils.unwrapObservable(valueAccessor()); // for dependency
// ensure that element is visible before trying to focus
setTimeout(function () {
ko.bindingHandlers.hasfocus.update(element, valueAccessor);
}, 0);
}
};
define(['bower_components/durandal/app',
define([
'bower_components/durandal/app',
'bower_components/durandal/viewLocator',
'bower_components/durandal/system',
'bower_components/durandal/plugins/router'],
function (app, viewLocator, system, router) {
'bower_components/durandal/plugins/router'
], function (app, viewLocator, system, router) {
//>>excludeStart("build", true);
system.debug(true);
......@@ -89,6 +91,7 @@
// this ensures that the title is simply the caption provided on the route
app.title = undefined;
app.start().then(function () {
// Replace 'viewmodels' in the moduleId with 'views' to locate the view.
// Look for partial views in a 'views' folder in the root.
......@@ -96,13 +99,14 @@
// configure routing
router.useConvention('js/viewmodels');
router.mapNav({
url: '/',
moduleId: 'js/viewmodels/todoapp',
name: 'TodoMVC',
caption: 'Durandal • TodoMVC'
});
router.mapNav({
url: '#/:filter',
moduleId: 'js/viewmodels/todoapp',
......@@ -117,4 +121,4 @@
app.setRoot('js/viewmodels/shell');
});
});
}());
\ No newline at end of file
})();
\ No newline at end of file
......@@ -2,17 +2,18 @@
define([
'bower_components/durandal/app'
], function (app) {
'use strict';
var ViewModel = function () {
var self = this;
// store the new todo value being entered
self.current = ko.observable();
// add a new todo, when enter key is pressed
self.add = function () {
var current = self.current().trim();
if (current) {
app.trigger('todoitem', current);
self.current('');
......
......@@ -3,7 +3,6 @@ define([
'bower_components/durandal/app',
'js/viewmodels/shell'
], function (app, shell) {
'use strict';
// represent a single todo item
......@@ -34,13 +33,16 @@ define([
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 () {
// 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()));
}).extend({
// save at most twice per second
throttle: 500
}); // save at most twice per second
});
};
......@@ -55,16 +57,16 @@ define([
self.filteredTodos = ko.computed(function () {
switch (self.showMode()) {
case 'active':
return self.todos().filter(function (todo) {
return !todo.completed();
});
case 'completed':
return self.todos().filter(function (todo) {
return todo.completed();
});
default:
return self.todos();
case 'active':
return self.todos().filter(function (todo) {
return !todo.completed();
});
case 'completed':
return self.todos().filter(function (todo) {
return todo.completed();
});
default:
return self.todos();
}
});
......@@ -75,7 +77,8 @@ define([
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;
// remove all completed todos
......@@ -106,7 +109,7 @@ define([
if (!self.cancelEdit) {
self.itemBeingEdited(undefined);
//trim and save back
// trim and save back
var trimmed = self.editTitle().trim();
item.title(trimmed);
......@@ -130,14 +133,16 @@ define([
// writeable computed observable to handle marking all complete/incomplete
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 () {
return !self.remainingCount();
},
// set all todos to the written value (true/false)
write: function (newValue) {
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);
});
}
......@@ -147,9 +152,7 @@ define([
self.getLabel = function (count) {
return ko.utils.unwrapObservable(count) === 1 ? 'item' : 'items';
};
};
return ViewModel;
});
/*global define*/
/*global define */
define([
'bower_components/durandal/plugins/router',
], function (router) {
'use strict';
return {
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 () {
return router.activate('todoapp');
}
......
......@@ -17,7 +17,7 @@
<footer id="footer" data-bind="visible: completedCount() || remainingCount()">
<span id="todo-count">
<strong data-bind="text: remainingCount">0</strong>
<span data-bind="text: getLabel( remainingCount )"></span> left
<span data-bind="text: getLabel(remainingCount)"></span> left
</span>
<ul id="filters">
<li>
......
<section id="todoapp">
<!--ko compose: {
model: 'js/viewmodels/entry', activate: true
}--><!--/ko-->
<div>
<!--ko compose: {
model: 'js/viewmodels/entry', activate: true
model: 'js/viewmodels/list', activate: true
}--><!--/ko-->
<div>
<!--ko compose: {
model: 'js/viewmodels/list', activate: true
}--><!--/ko-->
</div>
</div>
</section>
......@@ -31,4 +31,4 @@ _If you have other helpful links to share, or find any of the links above no lon
## 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