client/js/models.js
changeset 24 121a24be9da4
parent 23 70c8af9b44ec
child 27 6ec62ab8cb8d
--- a/client/js/models.js	Tue Aug 21 12:46:11 2012 +0200
+++ b/client/js/models.js	Tue Aug 21 16:27:26 2012 +0200
@@ -54,8 +54,16 @@
         prepare: function(options) {
             options.color = options.color || "#666666";
             return options;
-        }    
-    
+        },
+        toJSON: function() {
+            return {
+                id: this.get("id"),
+                title: this.get("title"),
+                uri: this.get("uri"),
+                description: this.get("description"),
+                color: this.get("color"),
+            }
+        },
     });
     
     // NODE
@@ -71,8 +79,17 @@
             this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user);
             options.description = options.description || "";
             return options;
-        }
-    
+        },
+        toJSON: function() {
+            return {
+                id: this.get("id"),
+                title: this.get("title"),
+                uri: this.get("uri"),
+                description: this.get("description"),
+                position: this.get("position"),
+                created_by: this.get("created_by").get("id")
+            }
+        },
     });
     
     // EDGE
@@ -101,7 +118,18 @@
             this.addReference(options, "from", project.get("nodes"), options.from);
             this.addReference(options, "to", project.get("nodes"), options.to);
             return options;
-        }
+        },
+        toJSON: function() {
+            return {
+                id: this.get("id"),
+                title: this.get("title"),
+                uri: this.get("uri"),
+                description: this.get("description"),
+                from: this.get("from").get("id"),
+                to: this.get("to").get("id"),
+                created_by: this.get("created_by").get("id"),
+            }
+        },
     });
         
     // PROJECT
@@ -160,6 +188,18 @@
         removeEdge: function(_model) {
             this.get("edges").remove(_model);
         },
+        validate: function(options) {
+            var _project = this;
+            _(options.users).each(function(_item) {
+                _item.project = _project;
+            });
+            _(options.nodes).each(function(_item) {
+                _item.project = _project;
+            });
+            _(options.edges).each(function(_item) {
+                _item.project = _project;
+            });
+        },
         // Add event handler to remove edges when a node is removed
         initialize: function() {
             var _this = this;