function Task(data) {
this.title = ko.observable(data.title);
this.isDone = ko.observable(data.isDone);
}
function TaskListViewModel() {
// Data that normally would come from the server
// (we need it here for the jsFiddle 'echo' system to simulate returned server data)
var fakeServerData = [
{ "title": "Wire the money to Panama", "isDone": true },
{ "title": "Get hair dye, beard trimmer, dark glasses and \"passport\"",
"isDone": false},
{ "title": "Book taxi to airport", "isDone": false },
{ "title": "Arrange for someone to look after the cat", "isDone": false }
];
// Data
var self = this;
self.tasks = ko.observableArray([]);
self.newTaskText = ko.observable();
self.incompleteTasks = ko.computed(function() {
return ko.utils.arrayFilter(self.tasks(), function(task) { return !task.isDone() && !task._destroy; });
});
var loadCount = 0;
// Operations
self.addTask = function() {
self.tasks.push(new Task({ title: this.newTaskText() }));
//self.tasks.push(new Task({ title: "task from VM", isDone: true }));
self.newTaskText("");
};
self.removeTask = function(task) {
//self.tasks.remove(task)
// This way adds a _destroy property to the task but does not actually remove it from the array.
// The server will use that info to remove its entry from the database.
self.tasks.destroy(task);
};
var loadFromServer = function() {
$.ajax("/echo/json/", {
data: { json: ko.toJSON(fakeServerData) },
type: "POST", dataType: 'json',
success: handleLoadFromServerResponse
});
};
// ajax success callback
function handleLoadFromServerResponse(allData) {
var mappedTasks = $.map(allData, function(item) { return new Task(item); });
debugger;
if(loadCount === 0)
self.tasks(mappedTasks);
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.