client/js/models.js
changeset 56 a9b9e6c7be63
parent 52 e0f6f3c31150
child 67 d341117f9370
--- a/client/js/models.js	Mon Feb 11 12:39:35 2013 +0100
+++ b/client/js/models.js	Mon Feb 11 18:08:19 2013 +0100
@@ -167,22 +167,22 @@
             },
           }
         ],
-        addUser: function(_props) {
+        addUser: function(_props, _options) {
             _props.project = this;
-            var _user = new User(_props);
-            this.get("users").push(_user);
+            var _user = User.findOrCreate(_props);
+            this.get("users").push(_user, _options);
             return _user;
         },
-        addNode: function(_props) {
-            _props.project = this;
-            var _node = new Node(_props);
-            this.get("nodes").push(_node);
+        addNode: function(_props, _options) {
+            _props.project = this;            
+            var _node = Node.findOrCreate(_props);
+            this.get("nodes").push(_node, _options);
             return _node;
         },
-        addEdge: function(_props) {
+        addEdge: function(_props, _options) {
             _props.project = this;
-            var _edge = new Edge(_props);
-            this.get("edges").push(_edge);
+            var _edge = Edge.findOrCreate(_props);
+            this.get("edges").push(_edge, _options);
             return _edge;
         },
         removeNode: function(_model) {
@@ -216,6 +216,55 @@
         }
     });
     
+    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,
+                site_id: this.get("site_id")
+            };
+        },
+    });
+    
+    var UsersList = Models.UsersList = Backbone.Collection.extend({
+    	model: RosterUser
+    });
+    
 
 }).call(window);