|
20
|
1 |
/* Saves the Full JSON at each modification */ |
|
|
2 |
|
|
|
3 |
Rkns.RemoteModels.FullJson = Rkns.Utils.inherit(Rkns.RemoteModels._Base); |
|
1
|
4 |
|
|
20
|
5 |
Rkns.RemoteModels.FullJson.prototype._init = function(_project, _opts) { |
|
|
6 |
this.url = _opts.url; |
|
|
7 |
this.load(this.url); |
|
|
8 |
if (typeof _opts.http_method == "undefined") { |
|
|
9 |
_opts.http_method = 'PUT'; |
|
|
10 |
} |
|
|
11 |
this.http_method = _opts.http_method; |
|
4
|
12 |
var _this = this; |
|
20
|
13 |
this.fullSave |
|
|
14 |
= this.addUser |
|
|
15 |
= this.addNode |
|
|
16 |
= this.addEdge |
|
|
17 |
= this.updateNode |
|
|
18 |
= this.updateEdge |
|
|
19 |
= this.removeNode |
|
|
20 |
= this.removeEdge |
|
|
21 |
= Rkns._.throttle(function() { |
|
|
22 |
_this._save.apply(this, Array.prototype.slice.call(arguments,0)); |
|
|
23 |
}, 2000); |
|
1
|
24 |
} |
|
|
25 |
|
|
20
|
26 |
Rkns.RemoteModels.FullJson.prototype.load = function(_url) { |
|
1
|
27 |
var _this = this; |
|
|
28 |
Rkns.$.getJSON(_url, function(_data) { |
|
|
29 |
_this.deserialize(_data); |
|
|
30 |
_this.handleCallbacks(); |
|
|
31 |
}); |
|
|
32 |
} |
|
|
33 |
|
|
20
|
34 |
Rkns.RemoteModels.FullJson.prototype.deserialize = function(_serializedData) { |
|
1
|
35 |
if (typeof _serializedData === "string") { |
|
|
36 |
_serializedData = JSON.parse(_serializedData); |
|
|
37 |
} |
|
|
38 |
var _proj = this._project; |
|
|
39 |
_proj.title = _serializedData.title || "(untitled project)"; |
|
|
40 |
if (typeof _serializedData.users === "object" && _serializedData.users) { |
|
4
|
41 |
Rkns._(_serializedData.users).each(function(_data) { |
|
|
42 |
var _userData = { |
|
|
43 |
id: _data.id, |
|
|
44 |
title: _data.title, |
|
|
45 |
uri: _data.uri, |
|
|
46 |
color: _data.color |
|
|
47 |
}; |
|
|
48 |
_proj.addUser(_userData); |
|
|
49 |
}); |
|
1
|
50 |
} |
|
|
51 |
if (typeof _serializedData.nodes === "object" && _serializedData.nodes) { |
|
4
|
52 |
Rkns._(_serializedData.nodes).each(function(_data) { |
|
|
53 |
var _nodeData = { |
|
|
54 |
id: _data.id, |
|
|
55 |
title: _data.title, |
|
5
|
56 |
description: _data.description, |
|
4
|
57 |
uri: _data.uri, |
|
|
58 |
created_by: _data.created_by, |
|
|
59 |
position: { |
|
|
60 |
x: _data.position.x, |
|
|
61 |
y: _data.position.y |
|
|
62 |
} |
|
|
63 |
}; |
|
|
64 |
_proj.addNode(_nodeData); |
|
|
65 |
}); |
|
1
|
66 |
} |
|
|
67 |
if (typeof _serializedData.edges === "object" && _serializedData.edges) { |
|
4
|
68 |
Rkns._(_serializedData.edges).each(function(_data) { |
|
|
69 |
var _edgeData = { |
|
|
70 |
id: _data.id, |
|
|
71 |
title: _data.title, |
|
|
72 |
uri: _data.uri, |
|
|
73 |
from: _data.from, |
|
|
74 |
to: _data.to, |
|
|
75 |
created_by: _data.created_by |
|
|
76 |
}; |
|
|
77 |
_proj.addEdge(_edgeData); |
|
|
78 |
}); |
|
1
|
79 |
} |
|
|
80 |
} |
|
4
|
81 |
|
|
20
|
82 |
Rkns.RemoteModels.FullJson.prototype.serialize = function() { |
|
4
|
83 |
var _res = { |
|
|
84 |
title: this._project.title, |
|
|
85 |
users: this._project.users.map(function(_user) { |
|
|
86 |
return { |
|
|
87 |
id: _user.id, |
|
|
88 |
title: _user.title, |
|
|
89 |
uri: _user.uri, |
|
|
90 |
color: _user.color |
|
|
91 |
} |
|
|
92 |
}), |
|
|
93 |
nodes: this._project.nodes.map(function(_node) { |
|
|
94 |
return { |
|
|
95 |
id: _node.id, |
|
|
96 |
title: _node.title, |
|
10
|
97 |
description: _node.description, |
|
4
|
98 |
uri: _node.uri, |
|
|
99 |
created_by: _node.created_by.id, |
|
|
100 |
position: { |
|
|
101 |
x: _node.position.x, |
|
|
102 |
y: _node.position.y |
|
|
103 |
} |
|
|
104 |
} |
|
|
105 |
}), |
|
10
|
106 |
edges: this._project.edges.map(function(_edge) { |
|
4
|
107 |
return { |
|
10
|
108 |
id: _edge.id, |
|
|
109 |
title: _edge.title, |
|
|
110 |
uri: _edge.uri, |
|
|
111 |
from: _edge.from.id, |
|
|
112 |
to: _edge.to.id, |
|
|
113 |
created_by: _edge.created_by.id |
|
4
|
114 |
} |
|
|
115 |
}) |
|
|
116 |
} |
|
|
117 |
return _res; |
|
|
118 |
} |
|
|
119 |
|
|
20
|
120 |
Rkns.RemoteModels.FullJson.prototype._save = function() { |
|
5
|
121 |
var _data = JSON.stringify(this.serialize()); |
|
13
|
122 |
Rkns.$.ajax({ |
|
20
|
123 |
type: this.http_method, |
|
|
124 |
url: this.url, |
|
13
|
125 |
contentType: "application/json", |
|
|
126 |
data: _data, |
|
|
127 |
success: function(data, textStatus, jqXHR) { |
|
4
|
128 |
} |
|
13
|
129 |
}); |
|
|
130 |
}; |