1 /* Saves the Full JSON at each modification */ |
1 /* Saves the Full JSON at each modification */ |
2 |
2 |
3 Rkns.RemoteModels.FullJson = Rkns.Utils.inherit(Rkns.RemoteModels._Base); |
3 Rkns.jsonIO = function(_renkan, _opts) { |
4 |
4 var _proj = _renkan.project; |
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") { |
5 if (typeof _opts.http_method == "undefined") { |
9 _opts.http_method = 'PUT'; |
6 _opts.http_method = 'PUT'; |
10 } |
7 } |
11 this.http_method = _opts.http_method; |
8 var _load = function() { |
12 var _this = this; |
9 Rkns.$.getJSON(_opts.url, function(_data) { |
13 this.fullSave |
10 _proj.get("users").add(_data.users); |
14 = this.addUser |
11 Rkns._(_data.nodes).each(function(_item) { |
15 = this.addNode |
12 _proj.addNode(_item); |
16 = this.addEdge |
13 }); |
17 = this.updateNode |
14 Rkns._(_data.edges).each(function(_item) { |
18 = this.updateEdge |
15 _proj.addEdge(_item); |
19 = this.removeNode |
16 }); |
20 = this.removeEdge |
17 }); |
21 = Rkns._.throttle(function() { |
18 } |
22 _this._save.apply(this, Array.prototype.slice.call(arguments,0)); |
19 var _save = function() { |
23 }, 2000); |
20 var _data = { |
24 } |
21 users: _proj.get("users").map(function(_item) { |
25 |
22 return { |
26 Rkns.RemoteModels.FullJson.prototype.load = function(_url) { |
23 id: _item.get("id"), |
27 var _this = this; |
24 title: _item.get("title"), |
28 Rkns.$.getJSON(_url, function(_data) { |
25 uri: _item.get("uri"), |
29 _this.deserialize(_data); |
26 description: _item.get("description"), |
30 _this.handleCallbacks(); |
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) { |
|
58 } |
|
59 }); |
|
60 |
|
61 } |
|
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 }); |
31 }); |
68 }); |
32 } |
69 } |
33 |
|
34 Rkns.RemoteModels.FullJson.prototype.deserialize = function(_serializedData) { |
|
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) { |
|
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 }); |
|
50 } |
|
51 if (typeof _serializedData.nodes === "object" && _serializedData.nodes) { |
|
52 Rkns._(_serializedData.nodes).each(function(_data) { |
|
53 var _nodeData = { |
|
54 id: _data.id, |
|
55 title: _data.title, |
|
56 description: _data.description, |
|
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 }); |
|
66 } |
|
67 if (typeof _serializedData.edges === "object" && _serializedData.edges) { |
|
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 }); |
|
79 } |
|
80 } |
|
81 |
|
82 Rkns.RemoteModels.FullJson.prototype.serialize = function() { |
|
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, |
|
97 description: _node.description, |
|
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 }), |
|
106 edges: this._project.edges.map(function(_edge) { |
|
107 return { |
|
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 |
|
114 } |
|
115 }) |
|
116 } |
|
117 return _res; |
|
118 } |
|
119 |
|
120 Rkns.RemoteModels.FullJson.prototype._save = function() { |
|
121 var _data = JSON.stringify(this.serialize()); |
|
122 Rkns.$.ajax({ |
|
123 type: this.http_method, |
|
124 url: this.url, |
|
125 contentType: "application/json", |
|
126 data: _data, |
|
127 success: function(data, textStatus, jqXHR) { |
|
128 } |
|
129 }); |
|
130 }; |
|