|
20
|
1 |
/* Saves the Full JSON at each modification */ |
|
|
2 |
|
|
23
|
3 |
Rkns.jsonIO = function(_renkan, _opts) { |
|
|
4 |
var _proj = _renkan.project; |
|
20
|
5 |
if (typeof _opts.http_method == "undefined") { |
|
|
6 |
_opts.http_method = 'PUT'; |
|
|
7 |
} |
|
23
|
8 |
var _load = function() { |
|
|
9 |
Rkns.$.getJSON(_opts.url, function(_data) { |
|
|
10 |
_proj.get("users").add(_data.users); |
|
|
11 |
Rkns._(_data.nodes).each(function(_item) { |
|
|
12 |
_proj.addNode(_item); |
|
|
13 |
}); |
|
|
14 |
Rkns._(_data.edges).each(function(_item) { |
|
|
15 |
_proj.addEdge(_item); |
|
|
16 |
}); |
|
4
|
17 |
}); |
|
1
|
18 |
} |
|
23
|
19 |
var _save = function() { |
|
|
20 |
var _data = { |
|
|
21 |
users: _proj.get("users").map(function(_item) { |
|
|
22 |
return { |
|
|
23 |
id: _item.get("id"), |
|
|
24 |
title: _item.get("title"), |
|
|
25 |
uri: _item.get("uri"), |
|
|
26 |
description: _item.get("description"), |
|
|
27 |
color: _item.get("color") |
|
|
28 |
} |
|
|
29 |
}), |
|
|
30 |
nodes: _proj.get("nodes").map(function(_item) { |
|
|
31 |
return { |
|
|
32 |
id: _item.get("id"), |
|
|
33 |
title: _item.get("title"), |
|
|
34 |
uri: _item.get("uri"), |
|
|
35 |
description: _item.get("description"), |
|
|
36 |
position: _item.get("position"), |
|
|
37 |
created_by: _item.get("created_by").get("id") |
|
|
38 |
} |
|
|
39 |
}), |
|
|
40 |
edges: _proj.get("edges").map(function(_item) { |
|
|
41 |
return { |
|
|
42 |
id: _item.get("id"), |
|
|
43 |
title: _item.get("title"), |
|
|
44 |
uri: _item.get("uri"), |
|
|
45 |
description: _item.get("description"), |
|
|
46 |
from: _item.get("from").get("id"), |
|
|
47 |
to: _item.get("to").get("id"), |
|
|
48 |
created_by: _item.get("created_by").get("id") |
|
|
49 |
} |
|
|
50 |
}) |
|
|
51 |
}; |
|
|
52 |
Rkns.$.ajax({ |
|
|
53 |
type: _opts.http_method, |
|
|
54 |
url: _opts.url, |
|
|
55 |
contentType: "application/json", |
|
|
56 |
data: JSON.stringify(_data), |
|
|
57 |
success: function(data, textStatus, jqXHR) { |
|
4
|
58 |
} |
|
23
|
59 |
}); |
|
|
60 |
|
|
4
|
61 |
} |
|
23
|
62 |
var _thrSave = Rkns._.throttle(_save, 2000); |
|
|
63 |
_load(); |
|
|
64 |
_proj.on("add:nodes add:edges add:users", function(_model) { |
|
|
65 |
_model.on("change remove", function(_model) { |
|
|
66 |
_thrSave(); |
|
|
67 |
}); |
|
|
68 |
}); |
|
4
|
69 |
} |