Created
August 21, 2012 14:35
-
-
Save bjartwolf/3416059 to your computer and use it in GitHub Desktop.
601 viewmodel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function Task(data) { | |
this.title = ko.observable(data.title); | |
this.isDone = ko.observable(data.isDone); | |
} | |
function TaskListViewModel() { | |
// Data | |
var self = this; | |
self.services = MYAPP.services; | |
self.tasks = ko.observableArray([]); | |
self.newTaskText = ko.observable(); | |
self.incompleteTasks = ko.computed(function () { | |
return ko.utils.arrayFilter(self.tasks(), function (task) { return !task.isDone(); }); | |
}); | |
// Operations | |
self.addTask = function () { | |
self.tasks.push(new Task({ title: this.newTaskText() })); | |
self.newTaskText(""); | |
}; | |
self.removeTask = function (task) { | |
self.tasks.remove(task); | |
}; | |
self.save = function () { | |
self.services.save(self.tasks(), function (err, result) { | |
if (err) { | |
console.log("Some error occured: " + err); | |
throw "Application crashed"; | |
} | |
console.log(result); | |
}); | |
}; | |
self.load = function () { | |
self.services.load(function (err, mappedTasks) { | |
if (err) { | |
console.log("Some error occured: " + err); | |
throw "Application crashed"; | |
} | |
self.tasks(mappedTasks); | |
}); | |
}; | |
self.load(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment