Commit 90878227 authored by ben hockey's avatar ben hockey

use toggle rather than ternary with add/remove

parent 236e44d6
/**
* Original source from https://gist.github.com/880822
/**
* Original source from https://gist.github.com/880822
* Converted to AMD-baseless format
*/
define(["dojo/_base/declare",
define(["dojo/_base/declare",
// Parent classes
"dijit/_WidgetBase", "dijit/_TemplatedMixin", "dijit/_WidgetsInTemplateMixin",
"dijit/_WidgetBase", "dijit/_TemplatedMixin", "dijit/_WidgetsInTemplateMixin",
// General application modules
"dojo/_base/lang", "dojo/_base/event", "dojo/on", "dojo/dom-class", "dojo/dom-attr", "dojox/mvc", "todo/model/TodoModel",
// Widget template
......@@ -15,7 +15,7 @@ define(["dojo/_base/declare",
return declare("todo.app", [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
templateString: template,
constructor: function () {
/**
* Create new application Model class, this will be used to bind
......@@ -25,14 +25,14 @@ define(["dojo/_base/declare",
this.model = new TodoModel();
/**
* The method below set up a function binding to the composite (complete & incomplete)
* The method below set up a function binding to the composite (complete & incomplete)
* model attributes. These values are used to append CSS classes to dynamically show & hide
* the "stats" elements when the model is non-empty and has some completed items. Whenever
* the "stats" elements when the model is non-empty and has some completed items. Whenever
* the values below are updated, the function will be executed.
*/
mvc.bindInputs([this.model.complete, this.model.incomplete], lang.hitch(this, "onItemStatusUpdate"));
/**
/**
* Hook into unload event to trigger persisting
* of the current model contents into the localStorage
* backed data store.
......@@ -43,11 +43,11 @@ define(["dojo/_base/declare",
},
/**
* Listen for item remove events from the using event delegation,
* Listen for item remove events from the using event delegation,
* we don't have to attach to each item. Also, ensure todo-stats
* have the correct initial CSS classes given the starting model
* contents.
*/
*/
postCreate: function () {
on(this.domNode, ".todo-destroy:click", lang.hitch(this, "onRemove"));
this.onItemStatusUpdate();
......@@ -55,35 +55,35 @@ define(["dojo/_base/declare",
/**
* Remove all items that have been completed from
* model. We have to individually check each todo
* model. We have to individually check each todo
* item, removing if true.
*/
removeCompletedItems: function () {
var len = this.model.todos.length, idx = 0;
/**
/**
* Removing array indices from a Dojo MVC Model
* array left-shifts the remaining items. When
* we find an item to remove, don't increment the
* array left-shifts the remaining items. When
* we find an item to remove, don't increment the
* index and, instead, decrement the total item count.
*/
while (idx < len) {
if (this.model.todos[idx].isDone.value) {
this.model.todos.remove(idx);
len--;
continue;
continue;
}
idx++;
idx++;
}
},
/**
* Add new a new todo item as the last element
* Add new a new todo item as the last element
* in the parent model.
*/
addToModel: function (content, isDone) {
var insert = mvc.newStatefulModel({
data: {todo_text: content, isDone: isDone}
data: {todo_text: content, isDone: isDone}
});
this.model.todos.add(this.model.todos.length, insert);
......@@ -92,17 +92,17 @@ define(["dojo/_base/declare",
/**
* Adjust CSS classes on todo-stats element based upon whether
* we a number of completed and incomplete todo items.
*/
*/
onItemStatusUpdate: function () {
domClass[this.model.complete.value > 0 ? "add" : "remove" ](this.todo_stats, "items_selected");
domClass[this.model.todos.get("length") ? "add" : "remove" ](this.todo_stats, "items_present");
domClass.toggle(this.todo_stats, "items_selected", this.model.complete.value > 0);
domClass.toggle(this.todo_stats, "items_present", this.model.todos.get("length"));
},
/**
* Handle key press events for the todo input
* field. If user has pressed enter, add current
* text value as new todo item in the model.
*/
*/
onKeyPress: function (event) {
if (event.keyCode !== 13) return;
......@@ -112,12 +112,12 @@ define(["dojo/_base/declare",
},
/**
* Event handler when user has clicked to
* Event handler when user has clicked to
* remove a todo item, just remove it from the
* model using the item identifier.
**/
**/
onRemove: function (event) {
this.model.todos.remove(domAttr.get(event.target, "data-model-id"));
this.model.todos.remove(domAttr.get(event.target, "data-model-id"));
}
});
});
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