client/js/full-json.js
changeset 23 70c8af9b44ec
parent 20 bd58970ffd16
child 24 121a24be9da4
--- a/client/js/full-json.js	Mon Aug 20 16:07:15 2012 +0200
+++ b/client/js/full-json.js	Tue Aug 21 12:46:11 2012 +0200
@@ -1,130 +1,69 @@
 /* Saves the Full JSON at each modification */
 
-Rkns.RemoteModels.FullJson = Rkns.Utils.inherit(Rkns.RemoteModels._Base);
-
-Rkns.RemoteModels.FullJson.prototype._init = function(_project, _opts) {
-    this.url = _opts.url;
-    this.load(this.url);
+Rkns.jsonIO = function(_renkan, _opts) {
+    var _proj = _renkan.project;
     if (typeof _opts.http_method == "undefined") {
         _opts.http_method = 'PUT';
     }
-    this.http_method = _opts.http_method;
-    var _this = this;
-    this.fullSave
-        = this.addUser
-        = this.addNode
-        = this.addEdge
-        = this.updateNode
-        = this.updateEdge
-        = this.removeNode
-        = this.removeEdge
-        = Rkns._.throttle(function() {
-            _this._save.apply(this, Array.prototype.slice.call(arguments,0));
-        }, 2000);
-}
-
-Rkns.RemoteModels.FullJson.prototype.load = function(_url) {
-    var _this = this;
-    Rkns.$.getJSON(_url, function(_data) {
-        _this.deserialize(_data);
-        _this.handleCallbacks();
-    });
-}
-
-Rkns.RemoteModels.FullJson.prototype.deserialize = function(_serializedData) {
-    if (typeof _serializedData === "string") {
-        _serializedData = JSON.parse(_serializedData);
-    }
-    var _proj = this._project;
-    _proj.title = _serializedData.title || "(untitled project)";
-    if (typeof _serializedData.users === "object" && _serializedData.users) {
-        Rkns._(_serializedData.users).each(function(_data) {
-            var _userData = {
-                id: _data.id,
-                title: _data.title,
-                uri: _data.uri,
-                color: _data.color
-            };
-            _proj.addUser(_userData);
-        });
-    }
-    if (typeof _serializedData.nodes === "object" && _serializedData.nodes) {
-        Rkns._(_serializedData.nodes).each(function(_data) {
-            var _nodeData = {
-                id: _data.id,
-                title: _data.title,
-                description: _data.description,
-                uri: _data.uri,
-                created_by: _data.created_by,
-                position: {
-                    x: _data.position.x,
-                    y: _data.position.y
-                }
-            };
-            _proj.addNode(_nodeData);
+    var _load = function() {
+        Rkns.$.getJSON(_opts.url, function(_data) {
+            _proj.get("users").add(_data.users);
+            Rkns._(_data.nodes).each(function(_item) {
+                _proj.addNode(_item);
+            });
+            Rkns._(_data.edges).each(function(_item) {
+                _proj.addEdge(_item);
+            });
         });
     }
-    if (typeof _serializedData.edges === "object" && _serializedData.edges) {
-        Rkns._(_serializedData.edges).each(function(_data) {
-            var _edgeData = {
-                id: _data.id,
-                title: _data.title,
-                uri: _data.uri,
-                from: _data.from,
-                to: _data.to,
-                created_by: _data.created_by
-            };
-            _proj.addEdge(_edgeData);
-        });
-    }
-}
-
-Rkns.RemoteModels.FullJson.prototype.serialize = function() {
-    var _res = {
-        title: this._project.title,
-        users: this._project.users.map(function(_user) {
-            return {
-                id: _user.id,
-                title: _user.title,
-                uri: _user.uri,
-                color: _user.color
+    var _save = function() {
+        var _data = {
+            users: _proj.get("users").map(function(_item) {
+                return {
+                    id: _item.get("id"),
+                    title: _item.get("title"),
+                    uri: _item.get("uri"),
+                    description: _item.get("description"),
+                    color: _item.get("color")
+                }
+            }),
+            nodes: _proj.get("nodes").map(function(_item) {
+                return {
+                    id: _item.get("id"),
+                    title: _item.get("title"),
+                    uri: _item.get("uri"),
+                    description: _item.get("description"),
+                    position: _item.get("position"),
+                    created_by: _item.get("created_by").get("id")
+                }
+            }),
+            edges: _proj.get("edges").map(function(_item) {
+                return {
+                    id: _item.get("id"),
+                    title: _item.get("title"),
+                    uri: _item.get("uri"),
+                    description: _item.get("description"),
+                    from: _item.get("from").get("id"),
+                    to: _item.get("to").get("id"),
+                    created_by: _item.get("created_by").get("id")
+                }
+            })
+        };
+        Rkns.$.ajax({
+            type: _opts.http_method,
+            url: _opts.url,
+            contentType: "application/json",
+            data: JSON.stringify(_data),
+            success: function(data, textStatus, jqXHR) {
             }
-        }),
-        nodes: this._project.nodes.map(function(_node) {
-            return {
-                id: _node.id,
-                title: _node.title,
-                description: _node.description,
-                uri: _node.uri,
-                created_by: _node.created_by.id,
-                position: {
-                    x: _node.position.x,
-                    y: _node.position.y
-                }
-            }
-        }),
-        edges: this._project.edges.map(function(_edge) {
-            return {
-                id: _edge.id,
-                title: _edge.title,
-                uri: _edge.uri,
-                from: _edge.from.id,
-                to: _edge.to.id,
-                created_by: _edge.created_by.id
-            }
-        })
+        });
+        
     }
-    return _res;
+    var _thrSave = Rkns._.throttle(_save, 2000);
+    _load();
+    _proj.on("add:nodes add:edges add:users", function(_model) {
+        _model.on("change remove", function(_model) {
+            _thrSave();
+        });
+    });
 }
-
-Rkns.RemoteModels.FullJson.prototype._save = function() {
-    var _data = JSON.stringify(this.serialize());
-    Rkns.$.ajax({
-    	type: this.http_method,
-    	url: this.url,
-    	contentType: "application/json",
-    	data: _data,
-    	success: function(data, textStatus, jqXHR) {
-        }
-    });
-};