server/src/main/webapp/js/models.js
changeset 47 267d67791e05
parent 46 7e132e2a48ca
child 48 01fb9167ad75
equal deleted inserted replaced
46:7e132e2a48ca 47:267d67791e05
     1 (function() {
       
     2 	
       
     3 	var root = this;
       
     4 		
       
     5 	var Backbone = root.Backbone;
       
     6 	
       
     7 	var Models = root.Rkns.Models = {};
       
     8 	
       
     9 	
       
    10 	Models.getUID = function(obj) {
       
    11 		var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
       
    12 		    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
       
    13 		    return v.toString(16);
       
    14 		});
       
    15 		return obj.type + "-" + guid; 
       
    16 	};
       
    17 	
       
    18 	
       
    19 	var RenkanModel = Backbone.RelationalModel.extend({
       
    20 		idAttribute : "_id",
       
    21 		constructor: function(options) {
       
    22 			
       
    23 			if (typeof options !== "undefined") {
       
    24 	            options._id = options._id || options.id || Models.getUID(this);
       
    25 	            options.title = options.title || "(untitled " + this.type + ")";
       
    26 	            options.description = options.description || "";
       
    27 	            options.uri = options.uri || "";
       
    28 	            
       
    29 	            if(typeof this.prepare === "function") {
       
    30 	            	options = this.prepare(options);
       
    31 	            }
       
    32 			}
       
    33 			Backbone.RelationalModel.prototype.constructor.call(this, options);
       
    34 		},
       
    35 		validate: function() {
       
    36 			if(!this.type) {
       
    37 				return "object has no type";
       
    38 			}
       
    39 		},
       
    40 		addReference : function(_options, _propName, _list, _id, _default) {
       
    41 		    var _element = _list.get(_id);
       
    42 		    if (typeof _element === "undefined" && typeof _default !== "undefined") {
       
    43 		        _options[_propName ] = _default;
       
    44 		    }
       
    45 		    else {
       
    46 		    	_options[_propName ] = _element;
       
    47 		    }
       
    48 		}
       
    49 	});
       
    50 	
       
    51 		
       
    52 	// USER
       
    53 	var User = Models.User = root.Rkns.Models.User = RenkanModel.extend({
       
    54 		type: "user",
       
    55 		prepare: function(options) {
       
    56 			options.color = options.color || "#666666";
       
    57 			return options;
       
    58 		}	
       
    59 	
       
    60 	});
       
    61 	
       
    62 	// NODE
       
    63 	var Node = Models.Node = root.Rkns.Models.Node = RenkanModel.extend({
       
    64 		type: "node",
       
    65 		relations: [{
       
    66 			type: Backbone.HasOne,
       
    67 			key: "created_by",
       
    68 			relatedModel: User,
       
    69 			reverseRelation : {
       
    70 				includeInJSON: '_id'
       
    71 			}
       
    72 		}],
       
    73 		prepare: function(options) {
       
    74 			project = options.project;
       
    75 			delete options["project"];
       
    76 			this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user);
       
    77 			options.description = options.description || "";			
       
    78 			return options;
       
    79 		}
       
    80 	
       
    81 	});
       
    82 	
       
    83 	// EDGE
       
    84 	var Edge = Models.Edge = root.Rkns.Models.Edge = RenkanModel.extend({
       
    85 		type: "edge",
       
    86 		relations: [
       
    87 		  {
       
    88 			type: Backbone.HasOne,
       
    89 			key: "created_by",
       
    90 			relatedModel: User,
       
    91 			reverseRelation : {
       
    92 				includeInJSON: '_id'
       
    93 			}
       
    94 		  },
       
    95 		  {
       
    96 		    type: Backbone.HasOne,
       
    97 			key: "from",
       
    98 			relatedModel: Node
       
    99 		  },
       
   100 		  {
       
   101 		    type: Backbone.HasOne,
       
   102 			key: "to",
       
   103 			relatedModel: Node
       
   104 		  },
       
   105 		],
       
   106 		prepare: function(options) {
       
   107 			project = options.project;
       
   108 			delete options.project;
       
   109 			this.addReference(options, "created_by", options.created_by, project.get("users"), project.current_user);
       
   110 			this.addReference(options, "from", options.from, project.get("nodes"));
       
   111 			this.addReference(options, "to", options.to, project.get("nodes"));
       
   112 			return options;
       
   113 		}
       
   114 	});
       
   115 		
       
   116 	// PROJECT
       
   117 	var Project = Models.Project = root.Rkns.Models.Project = RenkanModel.extend({
       
   118 		type: "project",
       
   119 		current_user : null,
       
   120 		relations: [
       
   121 		  {
       
   122 		    type: Backbone.HasMany,
       
   123 		    key: "users",
       
   124 		    relatedModel: User,
       
   125 		    reverseRelation: {
       
   126 	            key: 'project',
       
   127 	            includeInJSON: '_id'
       
   128 	        },
       
   129 		  },
       
   130 		  {
       
   131 		    type: Backbone.HasMany,
       
   132 		    key: "nodes",
       
   133 		    relatedModel: Node,
       
   134 		    reverseRelation: {
       
   135 	            key: 'project',
       
   136 	            includeInJSON: '_id'
       
   137 	        },
       
   138 		  },
       
   139 		  {
       
   140 		    type: Backbone.HasMany,
       
   141 		    key: "edges",
       
   142 		    relatedModel: Edge,
       
   143 		    reverseRelation: {
       
   144 	            key: 'project',
       
   145 	            includeInJSON: '_id'
       
   146 	        },
       
   147 		  }
       
   148 		],
       
   149 		addNode: function(_props) {
       
   150 			_props.project = this;
       
   151 			this.get("nodes").push(_props);
       
   152 		},
       
   153 		addUser: function(_props) {
       
   154 			this.get("users").push(_props);
       
   155 		}
       
   156 	});
       
   157 
       
   158 	
       
   159 }).call(this);
       
   160