|
1
|
1 |
Rkns.Serializers.BasicJson = Rkns.Utils.inherit(Rkns.Serializers._Base); |
|
|
2 |
|
|
|
3 |
Rkns.Serializers.BasicJson.prototype._init = function() { |
|
|
4 |
this.load(this._project._opts.url); |
|
4
|
5 |
var _this = this; |
|
|
6 |
this.save = Rkns._.throttle(function() { |
|
|
7 |
_this._save.apply(this, Array.prototype.slice.call(arguments,0)); |
|
|
8 |
}, 2000); |
|
1
|
9 |
} |
|
|
10 |
|
|
|
11 |
Rkns.Serializers.BasicJson.prototype.load = function(_url) { |
|
|
12 |
var _this = this; |
|
|
13 |
Rkns.$.getJSON(_url, function(_data) { |
|
|
14 |
_this.deserialize(_data); |
|
|
15 |
_this.handleCallbacks(); |
|
|
16 |
}); |
|
|
17 |
} |
|
|
18 |
|
|
|
19 |
Rkns.Serializers.BasicJson.prototype.deserialize = function(_serializedData) { |
|
|
20 |
if (typeof _serializedData === "string") { |
|
|
21 |
_serializedData = JSON.parse(_serializedData); |
|
|
22 |
} |
|
|
23 |
var _proj = this._project; |
|
|
24 |
_proj.title = _serializedData.title || "(untitled project)"; |
|
|
25 |
if (typeof _serializedData.users === "object" && _serializedData.users) { |
|
4
|
26 |
Rkns._(_serializedData.users).each(function(_data) { |
|
|
27 |
var _userData = { |
|
|
28 |
id: _data.id, |
|
|
29 |
title: _data.title, |
|
|
30 |
uri: _data.uri, |
|
|
31 |
color: _data.color |
|
|
32 |
}; |
|
|
33 |
_proj.addUser(_userData); |
|
|
34 |
}); |
|
1
|
35 |
} |
|
|
36 |
if (typeof _serializedData.nodes === "object" && _serializedData.nodes) { |
|
4
|
37 |
Rkns._(_serializedData.nodes).each(function(_data) { |
|
|
38 |
var _nodeData = { |
|
|
39 |
id: _data.id, |
|
|
40 |
title: _data.title, |
|
|
41 |
uri: _data.uri, |
|
|
42 |
created_by: _data.created_by, |
|
|
43 |
position: { |
|
|
44 |
x: _data.position.x, |
|
|
45 |
y: _data.position.y |
|
|
46 |
} |
|
|
47 |
}; |
|
|
48 |
_proj.addNode(_nodeData); |
|
|
49 |
}); |
|
1
|
50 |
} |
|
|
51 |
if (typeof _serializedData.edges === "object" && _serializedData.edges) { |
|
4
|
52 |
Rkns._(_serializedData.edges).each(function(_data) { |
|
|
53 |
var _edgeData = { |
|
|
54 |
id: _data.id, |
|
|
55 |
title: _data.title, |
|
|
56 |
uri: _data.uri, |
|
|
57 |
from: _data.from, |
|
|
58 |
to: _data.to, |
|
|
59 |
created_by: _data.created_by |
|
|
60 |
}; |
|
|
61 |
_proj.addEdge(_edgeData); |
|
|
62 |
}); |
|
1
|
63 |
} |
|
|
64 |
} |
|
4
|
65 |
|
|
|
66 |
Rkns.Serializers.BasicJson.prototype.serialize = function() { |
|
|
67 |
var _res = { |
|
|
68 |
title: this._project.title, |
|
|
69 |
users: this._project.users.map(function(_user) { |
|
|
70 |
return { |
|
|
71 |
id: _user.id, |
|
|
72 |
title: _user.title, |
|
|
73 |
uri: _user.uri, |
|
|
74 |
color: _user.color |
|
|
75 |
} |
|
|
76 |
}), |
|
|
77 |
nodes: this._project.nodes.map(function(_node) { |
|
|
78 |
return { |
|
|
79 |
id: _node.id, |
|
|
80 |
title: _node.title, |
|
|
81 |
uri: _node.uri, |
|
|
82 |
created_by: _node.created_by.id, |
|
|
83 |
position: { |
|
|
84 |
x: _node.position.x, |
|
|
85 |
y: _node.position.y |
|
|
86 |
} |
|
|
87 |
} |
|
|
88 |
}), |
|
|
89 |
edges: this._project.edges.map(function(_node) { |
|
|
90 |
return { |
|
|
91 |
id: _node.id, |
|
|
92 |
title: _node.title, |
|
|
93 |
uri: _node.uri, |
|
|
94 |
from: _node.from.id, |
|
|
95 |
to: _node.to.id, |
|
|
96 |
created_by: _node.created_by.id |
|
|
97 |
} |
|
|
98 |
}) |
|
|
99 |
} |
|
|
100 |
return _res; |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
Rkns.Serializers.BasicJson.prototype._save = function() { |
|
|
104 |
var _data = this.serialize(); |
|
|
105 |
Rkns.$.post( |
|
|
106 |
this._project._opts.url, |
|
|
107 |
_data, |
|
|
108 |
function(_res) { |
|
|
109 |
} |
|
|
110 |
); |
|
|
111 |
} |