client/js/models.js
changeset 299 c5086f714631
parent 298 2f35c2ae7de8
child 332 1a4b145c0607
equal deleted inserted replaced
298:2f35c2ae7de8 299:c5086f714631
   150                 type: Backbone.HasOne,
   150                 type: Backbone.HasOne,
   151                 key: "created_by",
   151                 key: "created_by",
   152                 relatedModel: User
   152                 relatedModel: User
   153             }
   153             }
   154         ],
   154         ],
       
   155         prepare: function(options) {
       
   156             var project = options.project;
       
   157             this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user);
       
   158             options.description = options.description || "";
       
   159             if(typeof options.offset !== "undefined") {
       
   160                 var offset = {};
       
   161                 if (Array.isArray(options.offset)) {
       
   162                   offset.x = options.offset[0];
       
   163                   offset.y = options.offset.length > 1 ? options.offset[1] : options.offset[0];
       
   164                 }
       
   165                 else if (options.offset.x != null) {
       
   166                   offset.x = options.offset.x;
       
   167                   offset.y = options.offset.y;
       
   168                 }
       
   169                 options.offset = offset;
       
   170             }
       
   171             return options;
       
   172         },
   155         toJSON: function() {
   173         toJSON: function() {
   156             return {
   174             return {
   157                 _id: this.get("_id"),
   175                 _id: this.get("_id"),
   158                 zoom_level: this.get("zoom_level"),
   176                 zoom_level: this.get("zoom_level"),
   159                 offset_x: this.get("offset_x"),
   177                 offset: this.get("offset"),
   160                 offset_y: this.get("offset_y"),
       
   161                 title: this.get("title"),
   178                 title: this.get("title"),
   162                 description: this.get("description"),
   179                 description: this.get("description"),
   163                 created_by: this.get("created_by") ? this.get("created_by").get("_id") : null
   180                 created_by: this.get("created_by") ? this.get("created_by").get("_id") : null
   164                 // Don't need project id
   181                 // Don't need project id
   165             };
   182             };
   239         removeEdge: function(_model) {
   256         removeEdge: function(_model) {
   240             this.get("edges").remove(_model);
   257             this.get("edges").remove(_model);
   241         },
   258         },
   242         validate: function(options) {
   259         validate: function(options) {
   243             var _project = this;
   260             var _project = this;
   244             _(options.users).each(function(_item) {
   261             _([].concat(options.users, options.nodes, options.edges, options.views)).each(function(_item) {
   245                 if(_item) {
       
   246                     _item.project = _project;
       
   247                 }
       
   248             });
       
   249             _(options.nodes).each(function(_item) {
       
   250                 if(_item) {
       
   251                     _item.project = _project;
       
   252                 }
       
   253             });
       
   254             _(options.edges).each(function(_item) {
       
   255                 if(_item) {
   262                 if(_item) {
   256                     _item.project = _project;
   263                     _item.project = _project;
   257                 }
   264                 }
   258             });
   265             });
   259         },
   266         },