server/src/main/webapp/static/js/models.js
changeset 51 3247fccfbd3f
parent 48 01fb9167ad75
child 54 43c35d6ca3f0
--- a/server/src/main/webapp/static/js/models.js	Tue Jan 01 09:28:03 2013 +0100
+++ b/server/src/main/webapp/static/js/models.js	Mon Jan 14 16:00:07 2013 +0100
@@ -167,19 +167,19 @@
         ],
         addUser: function(_props, _options) {
             _props.project = this;
-            var _user = new User(_props);
+            var _user = User.findOrCreate(_props);
             this.get("users").push(_user, _options);
             return _user;
         },
         addNode: function(_props, _options) {
-            _props.project = this;
-            var _node = new Node(_props);
+            _props.project = this;            
+            var _node = Node.findOrCreate(_props);
             this.get("nodes").push(_node, _options);
             return _node;
         },
         addEdge: function(_props, _options) {
             _props.project = this;
-            var _edge = new Edge(_props);
+            var _edge = Edge.findOrCreate(_props);
             this.get("edges").push(_edge, _options);
             return _edge;
         },
@@ -214,6 +214,54 @@
         }
     });
     
+    var RosterUser = Models.RosterUser = Backbone.Model.extend({
+        type: "roster_user",               
+        idAttribute : "_id",
+        
+        constructor: function(options) {
+            
+            if (typeof options !== "undefined") {
+                options._id = options._id || options.id || Models.getUID(this);
+                options.title = options.title || "(untitled " + this.type + ")";
+                options.description = options.description || "";
+                options.uri = options.uri || "";
+                options.project = options.project || null;
+                options.site_id = options.site_id || 0;
+                
+                if(typeof this.prepare === "function") {
+                    options = this.prepare(options);
+                }
+            }
+            Backbone.Model.prototype.constructor.call(this, options);
+        },
+        
+        validate: function() {
+            if(!this.type) {
+                return "object has no type";
+            }
+        },
+        
+        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"),
+                project: (this.get("project") != null)?this.get("project").get("id"):null
+            };
+        },
+    });
+    
+    var UsersList = Models.UsersList = Backbone.Collection.extend({
+    	model: RosterUser
+    });
+    
 
 }).call(window);