Skip to content
Permalink
Browse files

ajaxified load and save functions

  • Loading branch information...
Daniel Lerch
Daniel Lerch committed May 18, 2018
1 parent 1088200 commit 2f56b805a24c3d48f933662ece2e603cf7df3645
Showing with 72 additions and 6 deletions.
  1. +72 −6 src/TaskList.js
@@ -1,7 +1,7 @@
"use strict";

function TaskList(title) {
this.id = null;
this.id = title;
this.tasks = [];
this.title = title || "";
}
@@ -11,17 +11,83 @@ TaskList.prototype.size = function () {
};

TaskList.prototype.createTask = function (title) {
var _task = new Task(title);
this.tasks.push(_task);
return _task;
var task = new Task(title);
this.tasks.push(task);
return task;
};


TaskList.prototype.render = function () {
var $tasks = [];

$.each(this.tasks, function (index, task) {
$tasks.push(task.render());
});

return $('<ul>').append($tasks);
}
};


TaskList.prototype.toJSON = function () {
var jsonString = {
id: this.id,
title: this.title,
tasks: []
};
var i;
for (i = 0; i < this.tasks.length; i += 1) {
jsonString.tasks.push({
title: this.tasks[i].title,
done: this.tasks[i].done
});
}
return JSON.stringify(jsonString);
};

/*
* persists the tasklist to the server.
*
* for tasklists without id (not yet persisted) the id
* is written back to the model after it is received from
* the server.
*/
TaskList.prototype.save = function () {
var that = this;
var url = 'http://zhaw.herokuapp.com/task_lists/';
if (this.id) {
url += this.id;
}
$.post(url, this.toJSON(), function (returnedData) {
that.id = JSON.parse(returnedData).id;
window.location.hash = that.id;
});
};

// TaskList.prototype.shuffle = function () {
// var tasksArray = this.tasks;
// var i;
// for (i = 0; i < this.tasks.length; i += 1) {
// var checked = tasksArray[i].done;

// if (checked) {
// // tasksArray[i].appendTo("#taskList ul")
// }
// }
// };

/*
* Loads the given tasklist from the server.
*/
TaskList.load = function (id, callback) {
var taskList = new TaskList();
$.getJSON('http://zhaw.herokuapp.com/task_lists/' + id, function (returnedData) {
taskList.id = returnedData.id;
$.each(returnedData.tasks, function (index, task) {
var t = taskList.createTask(task.title);
t.done = task.done;
});
taskList.title = returnedData.title;
callback(taskList);
// taskList.shuffle();

});
};

0 comments on commit 2f56b80

Please sign in to comment.
You can’t perform that action at this time.