initialize data model and provide basic methods

pull/1/head
ansuz 7 years ago
parent 4345fc0662
commit 522476a2a6

@ -0,0 +1,70 @@
define([
], function () {
var Todo = {};
var Cryptpad;
/* data model
{
"order": [
"123456789abcdef0",
"23456789abcdef01",
"0123456789abcedf"
],
"data": {
"0123456789abcedf": {
"state": 0, // used to sort completed elements
"task": "pewpewpew",
"ctime": +new Date(), // used to display chronologically
"mtime": +new Date(), // used to display recent actions
// "deadline": +new Date() + 1000 * 60 * 60 * 24 * 7
},
"123456789abcdef0": {},
"23456789abcdef01": {}
}
}
*/
var initialize = function (proxy) {
// run migration
if (typeof(proxy.data) !== 'object') { proxy.data = {}; }
if (Array.isArray(proxy.order)) { proxy.order = []; }
if (typeof(proxy.type) !== 'string') { proxy.type = 'todo'; }
};
/* add (id, obj) push id to order, add object to data */
var add = function (proxy, id, obj) {
if (!Array.isArray(proxy.order)) {
throw new Error('expected an array');
}
proxy.order.push(id);
proxy.data[id] = obj;
};
/* delete (id) remove id from order, delete id from data */
var remove = function (proxy, id) {
if (Array.isArray(proxy.order)) {
var i = proxy.order.indexOf(id);
proxy.order.splice(i, 1);
}
if (proxy.data[id]) { delete proxy.data[id]; }
};
Todo.init = function (proxy, common) {
Cryptpad = common;
var api = {};
initialize(proxy);
api.add = function (id, obj) {
return add(proxy, id, obj);
};
api.remove = function (id) {
return remove(proxy, id);
};
return api;
};
return Todo;
});
Loading…
Cancel
Save