client/js/models.js
changeset 299 c5086f714631
parent 298 2f35c2ae7de8
child 332 1a4b145c0607
--- a/client/js/models.js	Sun May 25 13:45:24 2014 +0900
+++ b/client/js/models.js	Wed Jun 04 12:36:17 2014 +0200
@@ -152,12 +152,29 @@
                 relatedModel: User
             }
         ],
+        prepare: function(options) {
+            var project = options.project;
+            this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user);
+            options.description = options.description || "";
+            if(typeof options.offset !== "undefined") {
+                var offset = {};
+                if (Array.isArray(options.offset)) {
+                  offset.x = options.offset[0];
+                  offset.y = options.offset.length > 1 ? options.offset[1] : options.offset[0];
+                }
+                else if (options.offset.x != null) {
+                  offset.x = options.offset.x;
+                  offset.y = options.offset.y;
+                }
+                options.offset = offset;
+            }
+            return options;
+        },
         toJSON: function() {
             return {
                 _id: this.get("_id"),
                 zoom_level: this.get("zoom_level"),
-                offset_x: this.get("offset_x"),
-                offset_y: this.get("offset_y"),
+                offset: this.get("offset"),
                 title: this.get("title"),
                 description: this.get("description"),
                 created_by: this.get("created_by") ? this.get("created_by").get("_id") : null
@@ -241,17 +258,7 @@
         },
         validate: function(options) {
             var _project = this;
-            _(options.users).each(function(_item) {
-                if(_item) {
-                    _item.project = _project;
-                }
-            });
-            _(options.nodes).each(function(_item) {
-                if(_item) {
-                    _item.project = _project;
-                }
-            });
-            _(options.edges).each(function(_item) {
+            _([].concat(options.users, options.nodes, options.edges, options.views)).each(function(_item) {
                 if(_item) {
                     _item.project = _project;
                 }